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
index.ts
and:
  1. Use keyof to get the keys of an object type
  2. Use typeof to extract types from values
  3. Combine typeof with as const for literal types
  4. Create type-safe accessor functions

Please set the playground first

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