CodePark/ questions / keyof, typeof & indexed access
START PRACTICINGPRACTICE
Frontend question · TypeScript › keyof, typeof & indexed access

What is C?

const colors = { red: "#f00", blue: "#00f" } as const;
type C = keyof typeof colors;
"red" | "blue"

The answer is "red" | "blue". typeof colors gives the object type, and keyof takes its keys.

"#f00" | "#00f" is the tempting wrong answer, because those are the values, not the keys. To get the values, you would write (typeof colors)[keyof typeof colors].