TS ADVANCEDChapter 3 · TS Advanced
keyof and typeof
keyof T is the union of a type's property names: keyof { a: number; b: string } is "a" | "b". In a type position, typeof value gives you the type of a real value. Together they derive types from your data instead of writing them twice, so the types update automatically when the data changes.
Worked example
How it reads
typeof palettecopies the object's shape into a typekeyofturns that shape into"dusk" | "dawn" | "night"- Add a color to the object and
ColorNamegrows by itself

Cloud tip:
Object.keys always returns string[]. Cast it with as (keyof T)[] only when you know the object has no extra keys.

