Skip to content
TypeScript

マップ型

既存の型から新しい型を構築。

#mapped-type#advanced-type

Code

typescript
type Readonly<T> = {
  readonly [P in keyof T]: T[P];
};

type Optional<T> = {
  [P in keyof T]?: T[P];
};

type Mutable<T> = {
  -readonly [P in keyof T]: T[P];
};

type Nullable<T> = {
  [P in keyof T]: T[P] | null;
};

interface User { id: number; name: string; }
type ReadonlyUser = Readonly<User>;
type OptionalUser = Optional<User>;