ARTICLE AD BOX
Using TypeScript, I want to perform type checking on my application search results. Results will be split into categories.
Example response:
{ total: 25, results: { products: [ {}, {} ] } }products is the category. One category appears in the results at a time.
The problem: The property name must be restricted to known categories. Exactly one category will appear in the result.
Invalid code so far:
// Defining types const searchCategories = ['products', 'people'] as const; type SearchCategory = typeof searchCategories[number]; type CategorySearchResult<T extends SearchCategory> = { total: number, results: { // The part I can't figure out [T]: any[] } } // Declaring a result const r:CategorySearchResult<'products'> = { total: 25, results: { products: [ {}, {} ] } }Using Record<SearchCategory, any[]> seems wrong since it allows all categories to be entered, where I want only the generic type passed in allowed.
