Dmytro Morar
TypeScript

Mapped types

Mapped types build new types by iterating keys from another type.

Example

type User = {
  id: number;
  name: string;
  email: string;
};
type OptionalUser = {
  [K in keyof User]?: User[K];
};

Variations

type ReadonlyUser = {
  readonly [K in keyof User]: User[K];
};
type StringifyUser = {
  [K in keyof User]: string;
};

Generic template

type PartialType<T> = {
  [K in keyof T]?: T[K];
};

On this page