TypeSciprt 提供了几种实用类型,方便常见的类型转换。这些类型在全局可用。

原文链接:https://www.typescriptlang.org/docs/handbook/utility-types.html

Awaited<Type>

模拟 async function 中的 awaitPromises.then() 方法等操作,确切来说,就是递归展开Promises

1
2
3
4
5
6
7
8
9
10
11
12
13
type A = Awaited<Promise<string>> // type A = string

type B = Awaited<Promise<Promise<number>>> // type B = number

type C = Awaited<boolean | Promise<number>> // type C = boolean | number

function fn1(): Promise<string> {
return new Promise(resolve => {
resolve('success')
})
}

type D = Awaited<typeof fn1> // type D = string

Partial<Type>

构造一个将给定类型Type的所有属性设置为可选的类型,该实用类型返回给定类型的所有子集的类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface Todo {
title: string;
description: string;
}

function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
return { ...todo, ...fieldsToUpdate };
}

const todo1 = {
title: "organize desk",
description: "clear clutter",
};

const todo2 = updateTodo(todo1, {
description: "throw out trash",
});

Required<Type>

构造一个将给定类型Type的所有属性设置为必需的类型,和Partial相反。

1
2
3
4
5
6
7
8
9
interface Props {
a?: number;
b?: string;
}

const obj: Props = { a: 5 };

const obj2: Required<Props> = { a: 5 };
// Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.

Readonly<Type>

构造一个将给定类型Type的所有属性设置为只读的类型。将不能重新给所构造类型的属性分配值。

1
2
3
4
5
6
7
8
9
10
interface Todo {
title: string;
}

const todo: Readonly<Todo> = {
title: "Delete inactive users",
};

todo.title = "Hello";
// Cannot assign to 'title' because it is a read-only property.

Record<Keys, Type>

构造一个对象类型,其键的属性为Keys,值的属性为Type。这个工具类型可以用于将一个类型的属性映射到另一个类型。

1
2
3
4
5
6
7
8
9
10
11
12
interface CatInfo {
age: number;
breed: string;
}

type CatName = "miffy" | "boris" | "mordred";

const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: "Persian" },
boris: { age: 5, breed: "Maine Coon" },
mordred: { age: 16, breed: "British Shorthair" },
};

Pick<Type, Keys>

通过从给定的类型Type中选择一组属性Keys(字符串字面量或字符串字面量的联合)构建一个类型。

1
2
3
4
5
6
7
8
9
10
11
12
interface Todo {
title: string;
description: string;
completed: boolean;
}

type TodoPreview = Pick<Todo, "title" | "completed">;

const todo: TodoPreview = {
title: "Clean room",
completed: false,
};

Omit<Type, Keys>

通过从给定类型Tpye中移除Keys(字符串字面量或字符串字面量的联合)构造一个类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
interface Todo {
title: string;
description: string;
completed: boolean;
createdAt: number;
}

type TodoPreview = Omit<Todo, 'description'>;

const todo: TodoPreview = {
title: 'Clean room',
completed: false,
createdAt: 1615544252770
}

type TodoInfo = Omit<Todo, 'completed' | 'createdAt'>;

const todoInfo: TodoInfo = {
title: 'Pick up kids',
description: 'Kindergarten closes at 5pm'
}

Exclude<UnionType, ExcludedMembers>

通过从UnionType中排除可分配给ExcludedMembers的所有联合成员构造一个类型。

1
2
3
4
5
6
7
8
// type T0 = 'b' | 'c'
type T0 = Exclude<'a' | 'b' | 'c', 'a'>;

// type T1 = 'c'
type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>;

// type T2 = string | number
type T2 = Exclude<string | number | (() => void), Function>;

Extract<Type, Union>

通过从给定类型Type中提取可分配给Union的所有联合成员来构造一个类型。

1
2
3
4
5
// type T0 = "a"
type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>;

// type T1 = () => void
type T1 = Extract<string | number | (() => void), Function>;

NonNullable<Type>

通过从给定类型Type中排除null和undefined`来构造一个类型。

1
2
3
4
5
// type T0 = string | number
type T0 = NonNullable<string | number | undefined>;

// type T1 = string[]
type T1 = NonNullable<string[] | null | undefined>;

Parameters<Type>

从给定的函数类型Type的形参中使用的类型构造一个元组类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function f1(arg: { a: number; b: string }) {}

// type T0 = []
type T0 = Parameters<() => string>;

// type T1 = [s: string]
type T1 = Parameters<(s: string) => void>;

// type T2 = [arg: unknown]
type T2 = Parameters<<T>(arg: T) => T>;

// type T3 = [arg: { a: number; b: string }]
type T3 = Parameters<typeof f1>;

// type T4 = unknown[]
type T4 = Parameters<any>;

// type T5 = never
type T5 = Parameters<never>;

// Type 'string' does not satisfy the constraint '(...args: any) => any'.
type T6 = Parameters<string>;

// Type 'Function' does not satisfy the constraint '(...args: any) => any'.
// Type 'Function' provides no match for the signature '(...args: any): any'.
type T7 = Parameters<Function>;

ConstructorParameters<Type>

用给定的构造函数的类型Type构造元组或数组。生成一个具有所有形参类型的元组类型(如果Type不是函数,该类型为never)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// type T0 = [message?: string | undefined, options?: ErrorOptions | undefined]
type T0 = ConstructorParameters<ErrorConstructor>

// type T1 = string[]
type T1 = ConstructorParameters<FunctionConstructor>

// type T2 = [pattern: string | RegExp, flags?: string | undefined]
type T2 = ConstructorParameters<RegExpConstructor>

// type T3 = unknown[]
type T3 = ConstructorParameters<any>

// Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
// Type 'Function' provides no match for the signature 'new (...args: any): any'.
type T4 = ConstructorParameters<Function>

ReturnType<Type>

构造一个由给定函数类型Type的返回类型组成的类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const f1 = () => ({ a: 1, b: '' })

// type T0 = string
type T0 = ReturnType<() => string>;

// type T1 = void
type T1 = ReturnType<(s: string) => void>;

// type T2 = unknown
type T2 = ReturnType<<T>() => T>;

// type T3 = number[]
type T3 = ReturnType<<T extends U, U extends number[]>() => T>;

// type T4 = { a: number; b: string; }
type T4 = ReturnType<typeof f1>;

// type T5 = any
type T5 = ReturnType<any>;

// type T6 = never
type T6 = ReturnType<never>;

// Type 'string' does not satisfy the constraint '(...args: any) => any'.
type T7 = ReturnType<string>;

/** Type 'Function' does not satisfy the constraint '(...args: any) => any'.
Type 'Function' provides no match for the signature '(...args: any): any'. */
type T8 = ReturnType<Function>;

InstanceType<Type>

构造一个类型,由Type中构造函数实力类型组成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class C {
x = 0
y = 0
}

// type T0 = C
type T0 = InstanceType<typeof C>;

// type T1 = any
type T1 = InstanceType<any>;

// type T2 = never
type T2 = InstanceType<never>;

// Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
type T3 = InstanceType<string>;

/** Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
Type 'Function' provides no match for the signature 'new (...args: any): any'. */
type T4 = InstanceType<Function>;

ThisParameterType<Type>

提取函数类型Typethis形参的类型,如果Type没有此形参,则为unknown

1
2
3
4
5
6
7
function toHex(this: number) {
return this.toString()
}

function numberToString(n: ThisParameterType<typeof toHex>) {
return toHex.apply(n)
}

OmitThisParameter<Type>

从函数类型Type中移除this形参。如果Type没有显式声明this形参,结果就是Type。否则,将依照Type创建一个不带此形参的新函数。泛型被删除,只有最后一个重载签名被传到新的函数类型中。

1
2
3
4
5
6
7
function toHex(this: number) {
return this.toString(16)
}

const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5)

console.log(fiveToHex())

ThisType<Type>

该实用类型不返回转换后的类型。它是上下文this类型的标记。要使用这个实用类型,必须启用noImplicitThis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
type ObjectDescriptor<D, M> = {
data?: D;
methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
};

function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
const data: object = desc.data || {}
const methods: object = desc.methods || {}
return { ...data, ...methods } as D & M
}

const obj = makeObject({
data: { x: 0, y: 0 },
methods: {
moveBy(dx: number, dy: number) {
this.x += dx // Strongly typed this
this.y += dy // Strongly typed this
}
}
})

obj.x = 10
obj.y = 20
obj.moveBy(5, 5)

Uppercase<StringType>

将字符串中的每个字符串转换为大写。

1
2
3
4
5
6
7
8
9
type Greeting = 'Hello World'

// type ShoutyGreeting = "HELLO WORLD"
type ShoutyGreeting = Uppercase<Greeting>

type ASCIICacheKey<Str extends string> = `ID-${Uppercase<Str>}`

// type MainID = "ID-MY_APP"
type MainID = ASCIICacheKey<'my_app'>

Lowercase<StringType>

将字符串中的每个字符串转换为小写。

1
2
3
4
5
6
7
8
9
type Greeting = 'Hello World'

// type ShoutyGreeting = "hello world"
type ShoutyGreeting = Lowercase<Greeting>

type ASCIICacheKey<Str extends string> = `ID-${Lowercase<Str>}`

// type MainID = "ID-my_app"
type MainID = ASCIICacheKey<'my_app'>

Capitalize<StringType>

将字符串的第一个字符转为大写。

1
2
3
4
type LowercaseGreeting = "hello, world";

// type Greeting = "Hello, world"
type Greeting = Capitalize<LowercaseGreeting>;

Uncapitalize<StringType>

将字符串的第一个字符转为小写。

1
2
3
4
type UppercaseGreeting = "HELLO WORLD";

// type UncomfortableGreeting = "hELLO WORLD"
type UncomfortableGreeting = Uncapitalize<UppercaseGreeting>;