Generics and type-level tools · mid
keyof, typeof and indexed access
keyof T gives the union of T's keys, typeof value turns a value into its type, and T[K] reads a property's type, so you can derive types from one source of truth instead of repeating them.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
keyof { a: 1; b: 2 }is'a' | 'b'.typeof configin a type position gets the type of a runtime value; combine withas constto keep literals.T['key']andT[number](element type of an array/tuple):const roles = ['admin','user'] as const; type Role = typeof roles[number].- Deriving types from values keeps them in sync: change the array, the union changes.
The red pen
Common traps: mistake → fix
Mistake: "typeof in TypeScript is the JS typeof"
Fix: in a type position it means 'the type of this value', not the runtime string.
Easy to hard
Questions interviewers ask, easy to hard
- How do you get a union type from a const array?
- What does keyof return for an object with a string index signature?
- Derive the type of one property of an API response type.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.