infer
表示在extends条件语句中以占位符出现等到使用时才推断出来的数据类型
interface Customer {custname: string,buymoney: number
}
type CustFn = (params:Customer)=> string
type custParaTyp = CustFn extends (params: any) => infer R? R : CustFn// 运用在泛型
type ParamsTyp<T> = T extends (params: any) => infer R ? R :never
type CustParaTyp = ParamsTyp<CustFn> // stringtype EleOfArr<T> = T extends Array<infer P> ? P :never
type EleOfArrTest = EleOfArr<Array<number>> // number
TS 条件类型
// never 整体比较
type Test = string|number |boolean extends string |number ? string : never // 泛型迭代比较
type CondTyp<T> = T extends string |number ?T:never
type Test2 = CondTyp<string |number | boolean> // string | number
Extract
// type Extract<T, U> = T extends U ? T : never
type TestExtract1 = Extract<string, string | number> // string
type TestExtract2 = Extract<string |number |boolean, string |number> // string | number 迭代比较
Exclude
// type Exclude<T, U> = T extends U ? never : T
type TextExclude1 = Exclude<string, string | number> // never
type TextExclude2 = Exclude<string | number| boolean, string|number>// boolean
Record
type Record<K extends keyof any, T> = {[P in K]: T;
};type O = Record<string | number | symbol, any>
// key of any -> string | number | symbol
// P in K -> 迭代效果
Pick
抓取type类型,接口,类中的属性组成的一个新的对象类型
type Pick<T, K extends keyof T> = {[P in K]: T[P];
};
TS in 类型(映射类型)
type Omit1<T, K extends keyof T> = {[P in keyof T as P extends K?never:P]:T[P]
}
type Omit2<T, K extends keyof T> = {[P in keyof T as Exclude<P, K>]:T[P]
}
Required
去掉可选属性
type Required<T> = {[P in keyof T]-?: T[P];
};
Partial
全部转成可选属性
type Partial<T> = {[P in keyof T]?: T[P];
};
Readonly
不允许修改属性
type Readonly<T> = {readonly [P in keyof T]: T[P];
};// 去掉只读属性
type Readonly<T> = {- readonly [P in keyof T]: T[P];
};