Partial
将 type / interface 中每一个选项变成可选的。
Required
将 type / interface 中每一个选项变成必选的。
ReadOnly
将 type / interface 中每一个选项变成只读的。
Record
将Type 中的 key 作为类型的键,type 为类型的值,生成新的类型
interface Person {name:string,age:number}type qujunProps = Record<"qujun",Person>function getInfo(info:qujunProps){return info}getInfo({"qujun":{age:18,name:'hi'}})
Pick
在type 中指定属性,生成一个新的type
type Pick<T, K extends keyof T> = { [P in K]: T[P]; }
interface Person {name:string,age:number}type Pick2<T, K extends keyof T> = { [P in K]: T[P]; }type OnlyAge = Pick2<Person,"age">
Omit
在type 中移除指定的属性,生成一个新的 type
type Omit<T, K extends string | number | symbol> = {[P in Exclude<keyof T, K>]:T[P];}
Exclude
在联合类型中移除指定的类型值
type Exclude<T,P> = P extends T ? never: P
Extract
取两个类型的交集生成一个新的类型
type Extract<T,P> = T extends P ? T : never
NotNullable
移除联合类型中 undefined 和 null 的值
type NonNullable1<T> = T extends undefined|null ? never : T
Parame
将一个函数类型的type的参数类型返回成一个元组type Parameters<Textends (...args:any) => any> = Textends (...args:inferP) => any ? P :never;
问题:如何转成对象形式的 type 比如 T1 转成 :{s:string, k:number}
type T0 = Parameters<() => string>; // T0 = []type T1 = Parameters1<(s: string, k:number) => void>; T1= [s:string,k:number]
