keyof and typeof

Keyof Typeof
πŸ‘¨β€πŸ’Ό You've used type operators to extract and work with types!
πŸ¦‰ The as const trick is essential for deriving literal union types from arrays:
// Without as const
const colors = ['red', 'blue'] // Array<string>
type Color = (typeof colors)[number] // string 😒

// With as const
const colors = ['red', 'blue'] as const // readonly ['red', 'blue']
type Color = (typeof colors)[number] // 'red' | 'blue' πŸŽ‰
This pattern is used everywhere for configuration and constants.

Please set the playground first

Loading "keyof and typeof"
Loading "keyof and typeof"