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

What is K?

type Dict = { [key: string]: number };
type K = keyof Dict;
string | number

The answer is string | number. With a string index signature, keyof includes number too. JavaScript converts numeric keys to strings, so dict[0] is also allowed.

string is the tempting wrong answer. Note that a number index signature alone would give just number.