2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

この記事では、TypeScript の主要な Utility Types について記載します。Utility Types では、型から別の型を導き出すこと(型変換)ができます。

開発環境

開発環境は以下の通りです。

  • Windows11
  • VSCode
  • TypeScript 5.5.3

Partial<T>

全てのプロパティをオプションにします。

  • T : オブジェクト型
index.ts
type ToDo = {
  title: string;
  description: string;
};

type PartialToDo = Partial<ToDo>;

image.png

Required<T>

全てのプロパティを必須にします。Partial<T> の逆。

  • T : オブジェクト型
index.ts
type ToDo = {
  title: string;
  description?: string;
};

type RequiredToDo = Required<ToDo>;

image.png

Readonly<T>

全てのプロパティを読み取り専用にします。

  • T : オブジェクト型
index.ts
type ToDo = {
  title: string;
  description: string;
};

type ReadonlyToDo = Readonly<ToDo>;

image.png

読み取り専用 = 代入不可 になります。

  • 代入可
index.ts
const toDo: ToDo = {
  title: "cleaning",
  description: "clean my room",
};

toDo.description = "clean bathroom";
  • 代入不可
index.ts
const toDo: ReadonlyToDo = {
  title: "cleaning",
  description: "clean my room",
};

// Cannot assign to 'description' because it is a read-only property.ts(2540)
toDo.description = "clean bathroom";

readonly は指定したそのプロパティだけが読み取り専用になります。

index.ts
type Obj = {
  readonly toDo: ToDo;
};

const Object: Obj = {
  toDo: {
    title: "cleaning",
    description: "clean my room",
  },
};

// 代入不可
// Cannot assign to 'description' because it is a read-only property.ts(2540)
Object.toDo = {
  title: "shopping",
  description: "buy tomato",
};

// 代入可
Object.toDo.description = "clean bathroom";

Record<Kyes, Type>

プロパティのキーが Keys・バリュー(値)が Type であるオブジェクト型を作ります。

  • Kyes
    • オブジェクトのプロパティキーを指定
    • stringnumbersymbol とそれぞれのリテラル型が代入可
  • Type
    • オブジェクトのバリュー(値)の型を指定
    • 任意の型が代入可
index.ts
type ToDo = Record<"title" | "description", string>;

const toDo: ToDo = {
  title: "cleaning",
  description: "clean my room",
};

image.png

Kyesstring のリテラル型にした例

index.ts
type Member = "Wyatt" | "Billy";

type ToDo = {
  title: string;
  description: string;
};

type RecordToDo = Record<Member, ToDoB>;

const toDos: RecordToDo = {
  Wyatt: {
    title: "cleaning",
    description: "clean my room",
  },
  Billy: {
    title: "shopping",
    description: "buy tomato",
  },
};

image.png

Pick<T, Keys>

オブジェクト型 T から Keys に指定したキーだけを含むオブジェクト型を作ります。

  • T : オブジェクト型
  • Keys : T のプロパティキー
index.ts
type ToDo = {
  id: number;
  title: string;
  description: string;
  deadline: Date;
};

type PickToDo = Pick<ToDo, "title" | "description">;

image.png

Omit<T, Keys>

オブジェクト型 T から Keys に指定したキーを除いたオブジェクト型を作ります。Pick<T, Keys> の逆。

  • T : オブジェクト型
  • Keys : T のプロパティキー
index.ts
type ToDo = {
  id: number;
  title: string;
  description: string;
  deadline: Date;
};

type OmitToDo = Omit<ToDo, "title" | "description">;

image.png

Exclude<T, U>

ユニオン型 T から U に指定した型を取り除いたユニオン型を作ります。Omit<T, Keys> はオブジェクト型で、Exclude<T, U> はユニオン型。

  • T : ユニオン型
  • U : T から取り除きたい型
index.ts
type ToDo = "cleaning" | "shopping" | "washing" | "cooking";

type ExcludeToDo = Exclude<ToDo, "washing" | "cooking">;

image.png

ユニオン型の各型がオブジェクト型でも利用できます。

index.ts
type ToDo =
  | { title: "cleaning"; description: "clean my room" }
  | { title: "shopping"; description: "buy tomato" }
  | { title: "washing"; description: "wash t-shirt" }
  | { title: "cooking"; description: "cook dinner" };

type ExcludeToDo = Exclude<
  ToDo,
  | { title: "washing"; description: "wash t-shirt" }
  | { title: "cooking"; description: "cook dinner" }
>;

image.png

UT の部分集合ではないので、T 変更時、用途によっては U も変更する必要があります。
例えば、"cooking""packing" に変更し、"packing"ExcludeToDo に含めたくない場合、ExcludeToDo も変更する必要があります。

index.ts
type ToDo = "cleaning" | "shopping" | "washing" | "packing";

export type ExcludeToDo = Exclude<ToDo, "washing" | "cooking">;

image.png

Extract<T, U>

ユニオン型 T から U に指定した型を抽出したユニオン型を作ります。Exclude<T, U> の逆。Pick<T, Keys> はオブジェクト型で、Extract<T, U> はユニオン型。

  • T : ユニオン型
  • U : T から抽出したい型
index.ts
type ToDo = "cleaning" | "shopping" | "washing" | "cooking";

type ExtractToDo = Extract<ToDo, "washing" | "cooking">;

image.png

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?