前言
上一节我们封装了一些类型演算
本节课介绍的就是 ts 中自带的一些类型演算
关键字
Utility Types(类型演算、类型工具)
参考资料
- Utility Types:链接
notes
Utility Types
TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.
TypeScript 提供了一些类型工具促成常见的类型转换。这些工具可以在全局环境中访问。
Utility Types | 描述 |
---|---|
Partial |
将类型 T 中的成员变为可选 |
Required |
将类型 T 中的成员变为必填 |
Readonly |
将类型 T 中的成员变为只读 |
Exclude |
从 T 中剔除可以赋值给 U 的类型 |
Extract |
提取 T 中可以赋值给 U 的类型 |
NonNullable |
从 T 中剔除 null 和 undefined |
ReturnType |
获取函数返回值类型 |
InstanceType |
获取构造函数类型的实例类型 |
除了上述介绍的这些常用的类型工具,还有其它的一些类型工具,详情直接查阅官方文档:Utility Types
codes
源码
/**
* Make all properties in T optional
*/
type Partial<T> = {
[P in keyof T]?: T[P];
};
/**
* Make all properties in T required
*/
type Required<T> = {
[P in keyof T]-?: T[P];
};
/**
* Make all properties in T readonly
*/
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
/**
* Exclude from T those types that are assignable to U
*/
type Exclude<T, U> = T extends U ? never : T;
/**
* Extract from T those types that are assignable to U
*/
type Extract<T, U> = T extends U ? T : never;
/**
* Exclude null and undefined from T
*/
type NonNullable<T> = T extends null | undefined ? never : T;
/**
* Obtain the return type of a function type
*/
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
/**
* Obtain the return type of a constructor function type
*/
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
-?
表示去掉可选,变为必选。
示例
下面很多示例都来自于官方文档:Utility Types
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",
});
Constructs a type with all properties of Type set to optional. This utility will return a type that represents all subsets of a given type.
构造一个类型,该类型的所有属性都设置为可选。该类型工具将返回一个表示给定类型的所有子集的类型。
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>'.
Constructs a type consisting of all properties of Type set to required. The opposite of Partial.
构造一个类型,将所有属性设置为必填,该类型由这些必填属性组成。和该类型工具功能相反的是 Partial。
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.
Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.
构造一个类型,将所有属性设置为只读,该类型由这些只读属性组成,这意味着不能重新分配构造类型的属性(不能重新赋值)。
type T0 = Exclude<"a" | "b" | "c", "a">;
// => type T0 = "b" | "c"
type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
// => type T1 = "c"
type T2 = Exclude<string | number | (() => void), Function>;
// => type T2 = string | number
Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers.
构造一个类型,该类型中的成员存在于 UnionType 中,但不存在于 ExcludedMembers 中「1」。
type T = "男" | "女" | undefined | null;
type NEWT = Exclude<T, null | undefined>;
// => type NEWT = "男" | "女"
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
// => type T0 = "a"
type T1 = Extract<string | number | (() => void), Function>;
// => type T1 = () => void
Constructs a type by extracting from Type all union members that are assignable to Union.
构造一个类型,该类型中的成员从 Type 中提取,并且它们也存在于 Union 中「2」。
type T0 = NonNullable<string | number | undefined>;
// => type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;
// => type T1 = string[]
Constructs a type by excluding null and undefined from Type.
构造一个类型,该类型中不包含 null 和 undefined。
declare function f1(): { a: number; b: string };
type T0 = ReturnType<() => string>;
// => type T0 = string
type T1 = ReturnType<(s: string) => void>;
// => type T1 = void
type T2 = ReturnType<<T>() => T>;
// =>type T2 = unknown
type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
// => type T3 = number[]
type T4 = ReturnType<typeof f1>;
/*
type T4 = {
a: number;
b: string;
}
*/
type T5 = ReturnType<any>;
// => type T5 = any
type T6 = ReturnType<never>;
// => type T6 = never
type T7 = ReturnType<string>;
// => Type 'string' does not satisfy the constraint '(...args: any) => any'.
// => type T7 = any
type T8 = ReturnType<Function>;
// => Type 'Function' does not satisfy the constraint '(...args: any) => any'.
// => Type 'Function' provides no match for the signature '(...args: any): any'.
// => type T8 = any
Constructs a type consisting of the return type of function Type.
构造一个类型,该类型由函数的返回类型构成。
class C {
x = 0;
y = 0;
}
type T0 = InstanceType<typeof C>;
// => type T0 = C
type T1 = InstanceType<any>;
// => type T1 = any
type T2 = InstanceType<never>;
// => type T2 = never
type T3 = InstanceType<string>;
// => Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
// => type T3 = any
type T4 = InstanceType<Function>;
// => 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 = any
Constructs a type consisting of the instance type of a constructor function in Type.
构造一个类型,该类型由类的构造器所创建的实例类型构成。
class User {
loginid: string
}
class Article {}
type twoParamsConstructor = new (arg1: any, arg2: any) => User
type twoParamsConstructor2 = new (arg1: any, arg2: any) => Article
type Inst = InstanceType<twoParamsConstructor>;
// => type Inst = User
type Inst2 = InstanceType<twoParamsConstructor2>;
// => type Inst2 = Article