Mapped types
Map over keys
A mapped type builds a new object type by looping over the keys of an old one. The in keyword walks each key while keyof collects them.
Toggles turns every flag into a boolean switch. Add a key to Flags and the mapped type follows it at once.
type Flags = { dark: boolean; wide: boolean; }; type Toggles<T> = { [K in keyof T]: boolean; }; type FlagToggles = Toggles<Flags>; const toggles: FlagToggles = { dark: true, wide: false }; console.log(toggles.dark, toggles.wide);
true false
Add or remove ?
A question mark after the key list makes every property optional. The same spot also accepts a minus sign to strip optionality back off.
Optional needs no fields at all, which is handy for forms and patch objects. The value for each key stays exactly as it was.
type Flags = { dark: boolean; wide: boolean }; type Optional<T> = { [K in keyof T]?: T[K]; }; type Draft = Optional<Flags>; const empty: Draft = {}; console.log(Object.keys(empty).length);
0
Readonly maps
The readonly modifier works in the same key position and freezes every property. Frozen objects can be read but never reassigned.
origin reads fine, yet writing to origin.x fails at compile time. Use this shape for config and shared constants.
type Frozen<T> = { readonly [K in keyof T]: T[K]; }; type Point = { x: number; y: number }; const origin: Frozen<Point> = { x: 0, y: 0 }; console.log(origin.x, origin.y);
0 0
type Frozen<T> = { readonly [K in keyof T]: T[K] }; type Point = { x: number; y: number }; const origin: Frozen<Point> = { x: 0, y: 0 }; origin.x = 1; console.log(origin.x, origin.y);
main.ts(4,8): error TS2540: Cannot assign to 'x' because it is a read-only property.
Partial from scratch
The built-in Partial is just this pattern with a friendlier name. MyPartial maps each key to itself plus undefined and marks it optional.
Reading the hand-written version first makes every utility type later feel obvious. The next lessons put them all to work.
type MyPartial<T> = { [K in keyof T]?: T[K] | undefined; }; type Point = { x: number; y: number }; type HalfPoint = MyPartial<Point>; const half: HalfPoint = { x: 1 }; console.log(half.x, half.y);
1 undefined
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a mapped type do?
Answer
It builds a new object type by walking the keys of an old one
The
[K in keyof T]clause visits every key and gives it the type the mapping names. -
What does the
?after the key list add?Answer
Optionality for every mapped property
The question mark marks each property optional, so an empty object satisfies the type.
-
What does
readonlyin the key position do?Answer
It freezes every property against assignment
The modifier applies to each mapped property, and a write to one fails to compile.
-
What is the built-in
Partial<T>?Answer
A mapped type that makes every property optional
Partial is the mapped type with the question mark, under a name the standard library provides.
-
What does this program print?
type Flags = { dark: boolean; wide: boolean }; type Toggles<T> = { [K in keyof T]: boolean }; type FlagToggles = Toggles<Flags>; const toggles: FlagToggles = { dark: true, wide: false }; console.log(toggles.dark, toggles.wide);Answer
true false
The object supplies true for dark and false for wide, and the log prints them in order.
-
What does this program print?
type Flags = { dark: boolean; wide: boolean }; type Optional<T> = { [K in keyof T]?: T[K] }; type Draft = Optional<Flags>; const empty: Draft = {}; console.log(Object.keys(empty).length);Answer
0
Every property is optional, so the empty object is allowed and has no keys.
-
What does this program print?
type Frozen<T> = { readonly [K in keyof T]: T[K] }; type Point = { x: number; y: number }; const origin: Frozen<Point> = { x: 0, y: 0 }; console.log(origin.x, origin.y);Answer
0 0
Readonly does not change the values, so the log prints the two zeros.
-
What does this program print?
type MyPartial<T> = { [K in keyof T]?: T[K] | undefined }; type Point = { x: number; y: number }; type HalfPoint = MyPartial<Point>; const half: HalfPoint = { x: 1 }; console.log(half.x, half.y);Answer
1 undefined
The optional y was never set, so reading it gives undefined while x holds 1.
-
What does the compiler say about this program?
type Frozen<T> = { readonly [K in keyof T]: T[K] }; type Point = { x: number; y: number }; const origin: Frozen<Point> = { x: 0, y: 0 }; origin.x = 1; console.log(origin.x, origin.y);Answer
error TS2540: Cannot assign to 'x' because it is a read-only property.
The mapped type marked every property readonly, so the assignment is refused.
-
What does the compiler say about this program?
type Flags = { dark: boolean; wide: boolean }; type Toggles<T> = { [K in keyof T]: boolean }; type FlagToggles = Toggles<Flags>; const toggles: FlagToggles = { dark: "yes", wide: false }; console.log(toggles.dark, toggles.wide);Answer
error TS2322: Type 'string' is not assignable to type 'boolean'.
The mapping gave every key the type boolean, so a string cannot fill dark.
Article author: Arthur Isaev