Utility type 讲解 以下三个是最常用的
读取类型
1、Parameters 通过泛型 给传入一个类型,Parameters能够读出这个函数类型的参数型
return (...[endpoint, config]: Parameters<typeof http>) =>
http(endpoint, { ...config, token: user?.token });
变为可选
2、Partial 将其中的类型变为可选类型 非常方便
type Person = {
name: string;
age: number;
};
const xiaoming: Partial<Person> = { name: "xiaoming" };
删除类型
3、Omit<Person,any> 删除某些属性 从Person中删除哪些填入第二个 或第二个联合
const shenMiren: Omit<Person, "name"> = { age: 3 };
const shenMiren: Omit<Person, "name"> = { name: "qwe" }; //这个就报错了
const shenMiren: Omit<Person, "name" | "age"> = { name: "qwe" }; //这个就报错了
额外
//keyof
type PersonKeys = keyof Person; //把对象类型的键全部取出来,形成联合类型
//Pick
type PersonOnlyName = Pick<Person, "name">; //name必须是Person键值集合的子集
//Exclude
type Age = Exclude<PersonKeys, "name">; //形成了删除的效果