ConstructorParameters<T extends new (...args: any) => any>
1 2
type t = ConstructorParameters<(new (name: string) => any) | (new (age: number) => any)>; // type t = [string] | [number]
ReturnType
ReturnType<T>作用:获得函数返回值的类型。 源码:
1
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
1 2 3 4 5 6 7 8 9 10 11
type t = ReturnType<(name: string) => string | number>; // type t = string | number;
type T0 = ReturnType<() => string>; // string type T1 = ReturnType<(s: string) => void>; // void type T2 = ReturnType<<T>() => T>; // {} type T3 = ReturnType<<T extends U, U extends number[]>() => T>; // number[] type T4 = ReturnType<any>; // any type T5 = ReturnType<never>; // any type T6 = ReturnType<string>; // Error type T7 = ReturnType<Function>; // Error
InstanceType
源码:
1
InstanceType<T extends new (...args: any) => any>
InstanceType<T>用于获取构造函数的返回值类型。
1 2 3 4 5 6 7
type t = InstanceType<new (name: string) => {name: string, age: number}> /* 相当于 type t = { name: string; age: number; } */