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
{{ message }}
Repository navigation
Inference fails for generic-typed constructor parameter when no explicit constructor is present #47340
I'm trying to write a function that takes a (generic) constructor and calls it to instantiate a class. The type inference for such a function normally works fine, but if the constructor is for a generic class it fails even if the generic parameter has a default.
classFoo<Kextends{}={}>{}functioncreate<T>(type: new()=>T): T{returnnewtype();}letstring=create(String);// has type Stringletfoo=create(Foo);// should have type Foo<{}>, has type unknown
changed the title [-]Inference fails for generic-typed constructor parameter[/-][+]Inference fails for generic-typed constructor parameter when no explicit constructor is present[/+]on Jan 12, 2022
Activity
cijiugechu commentedon Jan 7, 2022
Probably because the class
Foodon't have a constructor,Kdidn't be consumed in the class.MartinJohns commentedon Jan 7, 2022
Use this instead:
Also, be wary of the
Stringtype. It's not assignable tostring.Use InstanceType to avoid microsoft/TypeScript#47340
Use InstanceType to avoid microsoft/TypeScript#47340 (#1765)
RyanCavanaugh commentedon Jan 12, 2022
This is a bug; inserting the constructor that's "there" by default
changes the behavior
[-]Inference fails for generic-typed constructor parameter[/-][+]Inference fails for generic-typed constructor parameter when no explicit constructor is present[/+]