4 ms·
You can also name your type with the same name as the value: export const Role = { CUSTOMER: 'customer', ADMIN: 'admin', SYSTEM: 'system',
by conaclos 5y ago
You can also name your type with the same name as the value:
export const Role = {
CUSTOMER: 'customer',
ADMIN: 'admin',
SYSTEM: 'system',
STAFF: 'staff',
} as const
export type Role = typeof Role[keyof typeof Role]
- joshstrange 5y ago:facepalm: of course I can, I don't know why it never occurred to me. Probably a case of "if it's ain't broke" but going forward I'll probably switch to this style.
- conaclos 5y agoFor a long time I avoided to use the same name for a type and a value because I was afraid of possible breaking change in the feature. And then I realized that this is an intended feature of TypeScript: type merging. Here the type `Role` merges with the type of the value `Role :)