型の操作
型から型を作成する
既存の型を合成、操作、変換することで、新しい型を作成できます。
交差型(&):
複数の型を単一の型に結合できます。
type A = { foo: number };type B = { bar: string };type C = A & B; // Intersection of A and Bconst obj: C = { foo: 42, bar: 'hello' };ユニオン型(|):
複数の型のうちいずれか 1 つになり得る型を定義できます。
type Result = string | number;const value1: Result = 'hello';const value2: Result = 42;マップ型:
既存の型のプロパティを変換して、新しい型を作成できます。
type Mutable<T> = { readonly [P in keyof T]: T[P];};type Person = { name: string; age: number;};type ImmutablePerson = Mutable<Person>; // Properties become read-only条件型:
何らかの条件に基づいて型を作成できます。
type ExtractParam<T> = T extends (param: infer P) => any ? P : never;type MyFunction = (name: string) => number;type ParamType = ExtractParam<MyFunction>; // stringインデックスアクセス型
TypeScript では、インデックス Type[Key] を使用して、別の型内のプロパティの型にアクセスし、操作できます。
type Person = { name: string; age: number;};
type AgeType = Person['age']; // numbertype MyTuple = [string, number, boolean];type MyType = MyTuple[2]; // booleanユーティリティ型
型の操作に使用できる組み込みユーティリティ型がいくつかあります。以下は、最もよく使用されるものの一覧です。
Awaited<T>
Promise 型を再帰的にアンラップする型を構築します。
type A = Awaited<Promise<string>>; // stringPartial<T>
T のすべてのプロパティをオプショナルに設定した型を構築します。
type Person = { name: string; age: number;};
type A = Partial<Person>; // { name?: string | undefined; age?: number | undefined; }Required<T>
T のすべてのプロパティを必須に設定した型を構築します。
type Person = { name?: string; age?: number;};
type A = Required<Person>; // { name: string; age: number; }Readonly<T>
T のすべてのプロパティを readonly に設定した型を構築します。
type Person = { name: string; age: number;};
type A = Readonly<Person>;
const a: A = { name: 'Simon', age: 17 };a.name = 'John'; // InvalidRecord<K, T>
T 型のプロパティ K の集合を持つ型を構築します。
type Product = { name: string; price: number;};
const products: Record<string, Product> = { apple: { name: 'Apple', price: 0.5 }, banana: { name: 'Banana', price: 0.25 },};
console.log(products.apple); // { name: 'Apple', price: 0.5 }Pick<T, K>
T から指定されたプロパティ K を選択して型を構築します。
type Product = { name: string; price: number;};
type Price = Pick<Product, 'price'>; // { price: number; }Omit<T, K>
T から指定されたプロパティ K を除外して型を構築します。
type Product = { name: string; price: number;};
type Name = Omit<Product, 'price'>; // { name: string; }Exclude<T, U>
T から U 型のすべての値を除外して型を構築します。
type Union = 'a' | 'b' | 'c';type MyType = Exclude<Union, 'a' | 'c'>; // bExtract<T, U>
T から U 型のすべての値を抽出して型を構築します。
type Union = 'a' | 'b' | 'c';type MyType = Extract<Union, 'a' | 'c'>; // a | cNonNullable<T>
T から null と undefined を除外して型を構築します。
type Union = 'a' | null | undefined | 'b';type MyType = NonNullable<Union>; // 'a' | 'b'Parameters<T>
関数型 T のパラメーター型を抽出します。
type Func = (a: string, b: number) => void;type MyType = Parameters<Func>; // [a: string, b: number]ConstructorParameters<T>
コンストラクター関数型 T のパラメーター型を抽出します。
class Person { constructor( public name: string, public age: number ) {}}type PersonConstructorParams = ConstructorParameters<typeof Person>; // [name: string, age: number]const params: PersonConstructorParams = ['John', 30];const person = new Person(...params);console.log(person); // Person { name: 'John', age: 30 }ReturnType<T>
関数型 T の戻り値の型を抽出します。
type Func = (name: string) => number;type MyType = ReturnType<Func>; // numberInstanceType<T>
クラス型 T のインスタンス型を抽出します。
class Person { name: string;
constructor(name: string) { this.name = name; }
sayHello() { console.log(`Hello, my name is ${this.name}!`); }}
type PersonInstance = InstanceType<typeof Person>;
const person: PersonInstance = new Person('John');
person.sayHello(); // Hello, my name is John!ThisParameterType<T>
関数型 T から ‘this’ パラメーターの型を抽出します。
interface Person { name: string; greet(this: Person): void;}type PersonThisType = ThisParameterType<Person['greet']>; // PersonOmitThisParameter<T>
関数型 T から ‘this’ パラメーターを削除します。
function capitalize(this: String) { return this[0].toUpperCase() + this.substring(1).toLowerCase();}
type CapitalizeType = OmitThisParameter<typeof capitalize>; // () => stringThisType<T>
コンテキストに基づく this 型のマーカーとして機能します。
type Logger = { log: (error: string) => void;};
let helperFunctions: { [name: string]: Function } & ThisType<Logger> = { hello: function () { this.log('some error'); // Valid as "log" is a part of "this". this.update(); // Invalid },};Uppercase<T>
入力型 T の名前を大文字にします。
type MyType = Uppercase<'abc'>; // "ABC"Lowercase<T>
入力型 T の名前を小文字にします。
type MyType = Lowercase<'ABC'>; // "abc"Capitalize<T>
入力型 T の名前の先頭を大文字にします。
type MyType = Capitalize<'abc'>; // "Abc"Uncapitalize<T>
入力型 T の名前の先頭を小文字にします。
type MyType = Uncapitalize<'Abc'>; // "abc"NoInfer<T>
NoInfer は、ジェネリック関数のスコープ内で型が自動推論されるのを阻止するために設計されたユーティリティ型です。
例:
// Automatic inference of types within the scope of a generic function.function fn<T extends string>(x: T[], y: T) { return x.concat(y);}const r = fn(['a', 'b'], 'c'); // Type here is ("a" | "b" | "c")[]NoInfer を使用する場合:
// Example function that uses NoInfer to prevent type inferencefunction fn2<T extends string>(x: T[], y: NoInfer<T>) { return x.concat(y);}
const r2 = fn2(['a', 'b'], 'c'); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.