Question presented to candidate:
"What is a mapped type in TypeScript? Show me how you would write Partial or Readonly yourself, and tell me what else mapped types can do."
What a strong answer should cover:
- Syntax
{ [K in Keys]: Type }: a type-level loop over a union of keys that produces one property per key, usually[K in keyof T]: T[K] - Changing value types (
T[K] | null, functions per property) and the modifiers: adding?/readonly, removing them with-?/-readonly(the+/-syntax arrived in TypeScript 2.8) - That
Partial,Required,Readonly,RecordandPickare mapped types in the standard library - Homomorphic mapped types (
in keyof T, orK extends keyof T) copy the original modifiers, work element-wise on arrays and tuples, and distribute over unions; mapping over an unrelated key union does not keep modifiers - Mapped types are compile-time only: building the matching run-time object still needs a loop (and usually one cast);
readonlyis not enforced at run time - Key remapping with
asexists for renaming or filtering keys (its own topic)
Code / implementation expected: Yes β implement Partial, Readonly or a Mutable helper with a mapped type.