Question presented to candidate:
"What does Record<K, V> do in TypeScript? When would you reach for it instead of writing an index signature or an object type by hand?"
What a strong answer should cover:
Record<K, V>builds an object type whose keys areKand whose values are allV; inlib.es5.d.tsit is the mapped type{ [P in K]: T }withK extends keyof any- With a union of literal keys every key is required and unknown keys are rejected, which gives exhaustiveness: adding a union member breaks every table that misses it
- With
stringkeys it is exactly an index signature, with the same "missing key is still typedV" gotcha - Common patterns: lookup tables of labels or handlers,
Partial<Record<K, V>>for optional keys, grouping results withRecord<string, T[]> - Limits:
Object.keysstill returnsstring[], and plain objects carry prototype keys;Mapis better for dynamic, user-supplied keys
Code / implementation expected: Yes β a lookup table keyed by a status union, and what tsc says when a key is missing.