Command Palette

Search for a command to run...

Hectal
Phase 8: Type Operators
TypeScript · Exercise 1 of 5Next
Beginner

Keys and values from a constant object

A palette object maps colour names to hex codes. The type ColorName should be the union of the palette's key names, and ColorHex the union of its values. Both must be **derived from palette**, not typed by hand.

Use keyof, typeof and indexed access ((typeof palette)[ColorName]). Then hexOf should compile and print #00ff00.

What Check tests

  1. Your code has no type errors
  2. The console prints #00ff00
  3. ColorName accepts "red"
  4. ColorName rejects "pink"
  5. ColorHex rejects a hex code that isn't in the palette

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Console

Nothing printed yet.