在映射类型里,新类型以相同的形式去转换旧类型的每个属性。
Partial
将每个属性转换为可选属性
type Partial<T> = {
[P in keyof T]?: T[P];
}
例子:
type PersonPartial = Partial<Person>;
// ^ = type PersonPartial = {
// name?: string | undefined;
// age?: number | undefined;
// }
Readonly
将每个属性转换为只读属性
type Readonly<T> = {
readonly [P in keyof T]: T[P];
}
例子:
type ReadonlyPerson = Readonly<Person>;
// ^ = type ReadonlyPerson = {
// readonly name: string;
// readonly age: number;
// }
Nullable
转换为旧类型和null的联合类型
type Nullable<T> = {
[P in keyof T]: T[P] | null
}
例子:
type NullablePerson = Nullable<Person>;
// ^ = type NullablePerson = {
// name: string | null;
// age: number | null;
// }
Pick
选取一组属性指定新类型
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
}
例子:
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick<Todo, "title" | "completed">;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
};
todo;
// ^ = const todo: TodoPreview
Record
创建一组属性指定新类型,常用来声明普通Object对象
type Record<K extends keyof any, T> = {
[P in K]: T;
}
例子:
interface PageInfo {
title: string;
}
type Page = "home" | "about" | "contact";
const nav: Record<Page, PageInfo> = {
about: { title: "about" },
contact: { title: "contact" },
home: { title: "home" },
};
nav.about;
// ^ = const nav: Record
Exclude
去除交集,返回剩余的部分
type Exclude<T, U> = T extends U ? never : T
例子:
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>'.
Omit
适用于键值对对象的Exclude,去除类型中包含的键值对
type Omit = Pick<T, Exclude<keyof T, K>>
例子:
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Omit<Todo, "description">;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
};
ReturnType
获取返回值类型,一般为函数
type ReturnType<T extends (...args: any) => any>
= T extends (...args: any) => infer R ? R : any;
例子:
declare function f1(): { a: number; b: string };
type T4 = ReturnType<typeof f1>;
// ^ = type T4 = {
// a: number;
// b: string;
// }
Required
将每个属性转换为必选属性
type Required<T> = {
[P in keyof T]-?: T[P]
}
例子:
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>'.
除此以外,还有很多类似的映射类型,可以参考[TypeScript: Documentation](TypeScript: Documentation - Utility Types (typescriptlang.org))获得更详细的信息。
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!