TypeScript·Intermediate·Coding·1 min read
How do keyof and indexed access types work together?
Short interview answer
keyof T produces the known property keys of T, and T[K] describes the value type at key K. A get function using K extends keyof T can return the precise property type rather than a broad union.
Example
function get<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key];}
const u = { id: 1, name: 'Ada' };get(u, 'name'); // string (not string | number)get(u, 'age'); // Error — not a key of uKey takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.