Open 1zumii opened 4 months ago
TestClass
instead of the class type type TestClas
as in TypeScript you could only get the prototype
property in the later one.Class[keyof Class]
, represents the instance type, equals with InstanceType<Class>
here.Updated code sample:
class TestClass {
private field: string = '';
func1(a: number): string {
return a.toString();
}
func2(b: boolean): boolean[] {
return [b, true, false];
}
}
type MethodParams<
Class extends abstract new (...args: any) => any,
Members = InstanceType<Class>
> = {
[K in keyof Members]: Members[K] extends (...args: any[]) => any
? Parameters<Members[K]>[0]
: never;
}[keyof Members];
type Result = MethodParams<typeof TestClass>;
Or use instance type directly:
// Or extends object if you want this tool type more generic
type MethodParams<Class extends InstanceType<typeof TestClass>> = {
[K in keyof Class]: Class[K] extends (...args: any) => any
? Parameters<Class[K]>[0]
: never;
}[keyof Class];
type Result = MethodParams<TestClass>;
🤔 i notice that InstanceType
just infer the matched type, when i navigate into TS sourcecode
**
* Obtain the return type of a constructor function type
*/
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
still confused at this part
Class[keyof Class]
, represents the instance type, equals withInstanceType<Class>
here.
class TestClass {
field: string = '';
}
type A = typeof TestClass; // The Constructor Type, only contains 'prototype' property ({ prototype: TestClass })
type B = TestClass; // The Instance Type
type Ins_A = InstanceType<A>; // InstanceType<Constructor Type> = Instance Type
type Z = A[keyof A]; // Equals with A['prototype'], which also equals with Instance Type
but
Result
inferred asnever
, notnumber | boolean
as expected