Utility Types 官网
//typeof 在ts中为静态环境时代码
//将func的参数拿出来作为泛型,然后转为类型;
const testFun = (b: Parameters<typeof func>) => {
}
// 将类型变为可选
type Person = {
name: string,
age: number
}
const xiaoMing: Partial<Person> = {name: "liu"}
// 源码:
// keyof 将key 都取出来
type Partial<T> = {
[P in keyof T]?: T[P];
};
// 将类型删除一些
type Person = {
name: string,
age: number,
tel: string
}
const shenMi: Omit<Person, 'name' | 'tel'> = {age: 36}
//源码
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
// Pick源码下面
//Pick 源码
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
// 挑选一些类型
type Person = {
name: string,
age: number,
tel: string
}
type pickTest = Pick<Person, 'name'>
// 过滤
//Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.
// 通过从类型中排除可分配给 ExcludedUnion 的所有联合成员来构造类型。
//源码
type Exclude<T, U> = T extends U ? never : T;
type Person = {
name: string,
age: number
}
type personKeys = keyof Person;
type Age = Exclude<personKeys, 'name'>
Extract
// Extract<Type, Union>
// Constructs a type by extracting from Type all union members that are assignable to Union.
// 通过从 Type 中提取可分配给 Union 的所有联合成员来构造类型。
// Example 例子
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
type T0 = "a"
type T1 = Extract<string | number | (() => void), Function>;
type T1 = () => void