Advertisement
Beginner TypeScript, Mapped Types

What is a mapped type, and what does the basic `{ [K in keyof T]: ... }` syntax mean?

Short answer

A mapped type generates a new object type by iterating over the keys of an existing type T (via `keyof T`) and applying a transformation to each property's type, letting you derive new types from existing ones without manually re-listing every property.

Save TypeScript mock interview
Advertisement
AModel answer
A mapped type generates a new object type by iterating over the keys of an existing type T (via `keyof T`) and applying a transformation to each property's type, letting you derive new types from existing ones without manually re-listing every property.
interface Person { name: string; age: number; }
type Stringify<T> = { [K in keyof T]: string };
type StringPerson = Stringify<Person>;
// { name: string; age: string }
Real-world example
Deriving a form-values type where every field of a data model is represented as a string (matching raw form input).
Likely follow-ups
  • What does 'keyof T' actually produce as a type on its own?
Related topics