Hacker News new | ask | show | jobs
by conaclos 1613 days 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]
1 comments

: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.
For 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 :)