Mapped type

Builds a new type by transforming each property of an existing type.

Since TS 2.1 Spec ↗

Syntax

{ [K in keyof T]: NewType }

Examples

type Optional<T> = { [K in keyof T]?: T[K] };
type Stringify<T> = { [K in keyof T]: string };
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};

Notes

Mapped types iterate over a key union with `in`. They can add or remove the `?` and `readonly` modifiers using `+`/`-`, and remap keys with an `as` clause. Most built-in utility types (`Partial`, `Readonly`, `Pick`) are mapped types.

See also