keyof and typeof
Keyof Typeof
π¨βπΌ Let's use
keyof and typeof to create type-safe utilities.const palette = {
primary: '#222222',
accent: '#ff6600',
}
type PaletteKey = keyof typeof palette
const sizes = ['sm', 'md', 'lg'] as const
type Size = (typeof sizes)[number]
π¨ Open
and:
- Use
keyofto get the keys of an object type - Use
typeofto extract types from values - Combine
typeofwithas constfor literal types - Create type-safe accessor functions


