extends
对于空对象{} 的比较
本质上类似于基类以及派生类,但空对象由于其内部无属性,任意一个对象(甚至是原始类型)都可以认为是它的子集。
type _T4 = {} extends {}? true: false;
// true
type _T5 = { name: 'linbudu'; } extends {} ? true: false;
// true
type _T6 = string extends {}? true: false;
// true
从extends视角看 unknown never any区别
以下表达式必定成立
- OtherType extends any
- never extends OtherType
- OtherType extends unknown
// extends 条件约束判断(子类型判断)
// 从extends视角看 unknown never any区别
type isA = { k: number; kk: string; kkk: boolean } extends aa ? number : string;
// unknown 是所有类型的父类型 topType
type TestUnknown = unknown extends aa ? number : string; // string
// never 是所有类型的子类型 bottomType
type TestUever = never extends aa ? number : string; // number
// any 是所有类型的联合类型 bottomType
type TestAny = any extends aa ? number : string; // string | number
// 联合分配类型: extends 关键字三元表达式 + 泛型
// 联合分配类型: 将单个联合类型分别带入条件,得到的结果再进行联合
// 分配率 (a+b) * c = ac + bc
type TestExtends<T> = T extends number ? number : string;
type TestExtendsType = TestExtends<number | string>; // string | number
泛型写法
type getParams<T> = T extends (...args: (infer P)[]) => any ? P : any
// function 1
function caller<T extends (...args: any[]) => any> (fn:T, parmas: getParams<T> ) : ReturnType<T> {
// return fn(parmas)
// }
// 箭头函数 1
const caller: <T extends (...args: any[]) => any>(fn:T, parmas: getParams<T> ) => ReturnType<T> = (fn, parmas) => fn(parmas)
// 箭头函数 2
type Caller= <T extends (...args: any[]) => any> (fn:T, parmas: getParams<T> ) => ReturnType<T>
const caller: Caller = (fn, parmas) => fn(parmas)
caller((ss: Number)=>ss, 33) // true
caller((ss: string)=>ss, 33) // false
分配条件类型: 泛型 + extends同时出现 会触发联合运算
When conditional types act on a generic type, they become distributive when given a union type
type A1 = 'x' extends 'x' ? string : number; // string
type A2 = 'x' | 'y' extends 'x' ? string : number; // number
type P<T> = T extends 'x' ? string : number;
type A3 = P<'x' | 'y'> // type A3 = string | number
//
keyof
type FunctionPropertyNames<T> = {[K in keyof T] : T[K] extends Function ? K : never }
var m = {
aa: () => {},
bb: () => {},
c:1,
d:1
}
type KK = FunctionPropertyNames<typeof m>
// {aa: 'aa', bb:'bb', c: never d: never}
type Keyof = keyof typeof m
// 'aa' | 'bb' | 'c' | 'd'
type km = KK[Keyof]
// 'aa' | 'bb'
// keyof 是对对属性的访问 对值为never类型是无法被访问的 可用于过滤
type FunctionPropertyNames<T> = {[K in keyof T] : T[K] extends Function ? K : never }[keyof T]
type GetFunctionProperty<T> = Pick<T, FunctionPropertyNames<T>>
var m = {
aa: () => {},
bb: () => {},
c:1,
d:1
}
type KK = GetFunctionProperty< typeof m>
// {aa: () => void; bb: () => void}
递归解析对象类型,去除function
// Combining all of the above to create a DeepReadonly<T> type
// that recursively makes all properties of an object read-only and removes all function properties (i.e. methods):
type DeepReadonly<T> = T extends any[] ? DeepReadonlyArray<T[number]> : T extends {} ? DeepReadonlyObjectNonFun<T> : T
type DeepReadonlyObject<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>
}
// type DeepReadonlyArray<T> = T extends (infer P)[] ? Array<DeepReadonly<P>> : never
type DeepReadonlyArray<T> = Array<DeepReadonly<T>>
type GetNonFunctionProperty<T> = {[P in keyof T] : T[P] extends Function ? never : P }[keyof T]
type PickNonFun<T> = Pick<T,GetNonFunctionProperty<T>>
type DeepReadonlyObjectNonFun<T> = DeepReadonlyObject<PickNonFun<T>>
var m = {
aa: () => {},
bb: () => {},
c:1,
d:1,
arr:[{
a1: 222,
afun: () => {}
}]
}
const ll = [{
aa:1
}]
type GetArrayType<T> = T extends any [] ? T[2] : never
type ak = GetArrayType<typeof ll>
// type ak = {
// aa: number;
// }
type MM = DeepReadonly<typeof m>
// type MM = {
// readonly c: DeepReadonlyObject<PickNonFun<number>>;
// readonly d: DeepReadonlyObject<PickNonFun<number>>;
// readonly arr: DeepReadonlyObject<...>[];
// }
type am = MM['arr'][0]
// type am = {
// readonly a1: DeepReadonlyObject<PickNonFun<number>>;
// }