0 of 6 problems solved0%
4 of 6

Color Lookup using typeof

as const freezes a value’s shape and keeps literal types. With const COLORS = {...} as const, keyof typeof COLORS is a union of color names and typeof COLORS[Name] is a union of the hex codes. Use these to type a lookup function that returns precise values.

const MAP = { a: 1, b: 2 } as const;
type Key = keyof typeof MAP; // 'a' | 'b'
type Val = (typeof MAP)[Key]; // 1 | 2

Return the exact hex string for the given color name.

Your Task

Map color names to hex codes with precise types.

  • Declare const COLORS = { primary: '#3366ff', danger: '#ff3355', neutral: '#888888' } as const.
  • Define type ColorName = keyof typeof COLORS and type ColorHex = typeof COLORS[ColorName].
  • Implement function getColor(name: ColorName): ColorHex by reading from COLORS[name].
  • Do not construct strings manually; read from the map.