はじめに
この記事では、TypeScript の主要な Utility Types について記載します。Utility Types では、型から別の型を導き出すこと(型変換)ができます。
開発環境
開発環境は以下の通りです。
- Windows11
- VSCode
- TypeScript 5.5.3
Partial<T>
全てのプロパティをオプションにします。
-
T
: オブジェクト型
type ToDo = {
title: string;
description: string;
};
type PartialToDo = Partial<ToDo>;
Required<T>
全てのプロパティを必須にします。Partial<T>
の逆。
-
T
: オブジェクト型
type ToDo = {
title: string;
description?: string;
};
type RequiredToDo = Required<ToDo>;
Readonly<T>
全てのプロパティを読み取り専用にします。
-
T
: オブジェクト型
type ToDo = {
title: string;
description: string;
};
type ReadonlyToDo = Readonly<ToDo>;
読み取り専用 = 代入不可 になります。
- 代入可
const toDo: ToDo = {
title: "cleaning",
description: "clean my room",
};
toDo.description = "clean bathroom";
- 代入不可
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
は指定したそのプロパティだけが読み取り専用になります。
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
- オブジェクトのプロパティキーを指定
-
string
、number
、symbol
とそれぞれのリテラル型が代入可
-
Type
- オブジェクトのバリュー(値)の型を指定
- 任意の型が代入可
type ToDo = Record<"title" | "description", string>;
const toDo: ToDo = {
title: "cleaning",
description: "clean my room",
};
Kyes
を string
のリテラル型にした例
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",
},
};
Pick<T, Keys>
オブジェクト型 T
から Keys
に指定したキーだけを含むオブジェクト型を作ります。
-
T
: オブジェクト型 -
Keys
:T
のプロパティキー
type ToDo = {
id: number;
title: string;
description: string;
deadline: Date;
};
type PickToDo = Pick<ToDo, "title" | "description">;
Omit<T, Keys>
オブジェクト型 T
から Keys
に指定したキーを除いたオブジェクト型を作ります。Pick<T, Keys>
の逆。
-
T
: オブジェクト型 -
Keys
:T
のプロパティキー
type ToDo = {
id: number;
title: string;
description: string;
deadline: Date;
};
type OmitToDo = Omit<ToDo, "title" | "description">;
Exclude<T, U>
ユニオン型 T
から U
に指定した型を取り除いたユニオン型を作ります。Omit<T, Keys>
はオブジェクト型で、Exclude<T, U>
はユニオン型。
-
T
: ユニオン型 -
U
:T
から取り除きたい型
type ToDo = "cleaning" | "shopping" | "washing" | "cooking";
type ExcludeToDo = Exclude<ToDo, "washing" | "cooking">;
ユニオン型の各型がオブジェクト型でも利用できます。
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" }
>;
Extract<T, U>
ユニオン型 T
から U
に指定した型を抽出したユニオン型を作ります。Exclude<T, U>
の逆。Pick<T, Keys>
はオブジェクト型で、Extract<T, U>
はユニオン型。
-
T
: ユニオン型 -
U
:T
から抽出したい型
type ToDo = "cleaning" | "shopping" | "washing" | "cooking";
type ExtractToDo = Extract<ToDo, "washing" | "cooking">;