Deriving enums from objects with TypeScript
TypeScript enums have some issues
https://react-typescript-cheatsheet.netlify.app/docs/basic/troubleshooting/types/#enum-types
Often a string union is sufficient
To simplify code, we can derive type unions from objects!
https://youtu.be/6M9aZzm-kEc?t=260
const routes = {
home: '/',
admin: '/admin',
users: '/users',
} as const;
Derive the keys of the object
ie `'home' | '/admin' | '/users'`
Derive the keys of the object
type RouteKeys = keyof typeof routes
Derive the values of the object
i.e. '/' | '/admin' | '/users'
Derive the values of the object
type RouteValues = (typeof routes)[keyof typeof routes]