You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeObjectType<T>=Textends(new(...args: any[])=> infer C) ? C : never;typeConstructArguments<T>=Textends(new(...args: infer C)=>any) ? C : never;declarefunctionClassWrapper<Classextends(new(...args: any[])=>any),OutputextendsObjectType<Class>,ConstructorArgsextendsConstructArguments<Class>>(base: Class): ((new(...args: ConstructorArgs)=>{c: string}&Output));vartest=ClassWrapper(Test);//Error about abstract class not being able to be assigned to new(): Test
Constructor arguments can not be inferred via crawling the prototype:
typeAbstractConstructArguments<T>=Textends{prototype:{constructor: (...args: infer C)=>any}}?C: never;varargs: AbstractConstructArguments<Test>;//args is never.
Expected behavior:
There should be a way to type the constructor arguments for an abstract class. Either the class is assignable to new or inferring the arguments from the constructor directly should result in valid arguments and not type never.
Actual behavior: ClassWrapper(Test) throws an error. AbstractConstructArguments always returns type never.
TypeScript Version: 4.0.3
Search Terms:
Infer arguments from constructor of abstract class.
Code
I have the following class, I cannot get the constructor parameters via generic typing.
Abstract classes cannot be assigned to new:
Constructor arguments can not be inferred via crawling the prototype:
Expected behavior:
There should be a way to type the constructor arguments for an abstract class. Either the class is assignable to new or inferring the arguments from the constructor directly should result in valid arguments and not type never.
Actual behavior:
ClassWrapper(Test)
throws an error.AbstractConstructArguments
always returns typenever
.Playground Link
Related Issues:
https://stackoverflow.com/questions/57643184/infer-arguments-for-abstract-class-in-typescript
The text was updated successfully, but these errors were encountered: