Reputation: 443
I have defined the following type:
type UserConfigCategories = 'tables-config' | 'link-config' | 'general-config';
however this type can grow and I do not want to have to update it. Is it possible to create this type dynamically from an array, for example?
Upvotes: 0
Views: 2617
Reputation: 3506
If the array defining the categories is a literal somewhere in your source you can infer the type from the values with as const
added in 3.4.
const CONFIG_CATEGORIES = ['tables-config', 'link-config', 'general-config'] as const;
type UserConfigCategories = (typeof CONFIG_CATEGORIES)[number]
const u: UserConfigCategories = 'tables-config';
const v: UserConfigCategories = 'bad-name'; // Type '"bad-name"' is not assignable to type
// '"tables-config" | "link-config" | "general-config"'.
Upvotes: 4