Sub topic 2
Mapped types: transforming every property
This chapter assumes you know basic object types, generics with <T>, and type aliases. You should also be comfortable reading keyof, which produces the union of an object type's property names.
A mapped type builds a new object type by looping over a set of keys and computing a property type for each one. The syntax is { [K in Keys]: ValueType }, where K is a type variable that takes each key in turn. Because keyof T is the most common source of keys, mapped types are the main tool for transforming every property of an existing type.
Reading keys and property types
Two operators do most of the work. keyof T gives the union of property names, and T[K] gives the type of the property named K.
type User = { id: number; name: string };
type UserKeys = keyof User; // "id" | "name"
type IdType = User["id"]; // numberInside a mapped type, K takes each member of keyof T, and T[K] gives that property's type.
Writing a mapped type
This mapped type reproduces Partial<T>, making every property optional:
type MyPartial<T> = { [K in keyof T]?: T[K] };
type User = { id: number; name: string };
type PartialUser = MyPartial<User>;
// { id?: number; name?: string }The ? after K is a modifier. Without it, the mapped type would keep each property as required.
Adding and removing modifiers
A mapped type can add or remove readonly and ? with a prefix. + adds a modifier, and - removes it. Writing no prefix leaves the original modifier in place.
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
type Concrete<T> = { [K in keyof T]-?: T[K] };
type Config = { readonly host: string; port?: number };
type MutableConfig = Mutable<Config>;
// { host: string; port?: number }
type ConcreteConfig = Concrete<Config>;
// { readonly host: string; port: number }-? removes optionality, so port becomes required. It does not remove undefined from the type. If the original property was declared port?: number, the mapped result is port: number, which still may not be enforceable at runtime.
Homomorphic mapped types
A mapped type written as [K in keyof T] is called homomorphic. TypeScript treats it specially: it copies the modifiers from T onto the result. This is why Concrete<Config> keeps readonly on host. A mapped type over a plain union of literals, such as [K in "a" | "b"], is not homomorphic and does not copy modifiers.
> Callout: Use keyof T directly in the in clause when you want to preserve modifiers. Replacing it with another key union drops them.
Walkthrough: a nullable version of any type
This example applies a transformation to every property: it widens each value type to also accept null.
type Nullable<T> = { [K in keyof T]: T[K] | null };
type Form = { email: string; age: number };
type OptionalForm = Nullable<Form>;
// { email: string | null; age: number | null }
const draft: OptionalForm = { email: null, age: 30 }; // validThe mapped type reads each property's type through T[K], so you never have to list the properties by hand. If you add a property to Form later, Nullable<Form> updates with it.
Common errors
- Forgetting that
-?does not removeundefinedfrom the value type when the property was declared with?andexactOptionalPropertyTypesis off. - Writing
[K in T]instead of[K in keyof T]. The first form fails to compile becauseTis not a key union. - Expecting
readonlyto be copied when the key set is notkeyof T.
Exercise
Exercise
Write a type Getters<T> that maps each property K of T to a function returning T[K], so that Getters<{ id: number }> equals { id: () => number }.
Show answer
type Getters<T> = { [K in keyof T]: () => T[K] };