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
- Your code has no type errors
- The console prints
#00ff00 ColorNameaccepts"red"ColorNamerejects"pink"ColorHexrejects 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.
Press Run to see the page.
Console
Nothing printed yet.