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
No way to get generic type of an abstract class inside a factory function #35576
typeInstanceViaNew<T>=Textends{new(...args: never[]): infer R} ? R : nevertypeInstanceViaProto<T>=Textends{prototype: infer R} ? R : neverfunctionclassFactory1<T>(){classCS{x!: T}returnCS}constCSS=classFactory1<string>()typeCS1=InstanceViaNew<typeofCSS>["x"]// ok, stringtypeCS2=InstanceViaProto<typeofCSS>["x"]// not ok, any :(functionclassFactory2<T>(){abstractclassACS{x!: T}returnACS}constACS=classFactory2<string>()typeAC1=InstanceViaNew<typeofACS>["x"]// not ok, never (because abstract classes have no new) :(typeAC2=InstanceViaProto<typeofACS>["x"]// not ok, any :(
Expected behavior:
There should be a way to make abstract classes keep the generic information and use it.
Actual behavior:
There's no way to make abstract classes keep the generic information and use it (unless they are type-casted to non abstract classes by adding to them a fake constructor).
Also I found out that if you do something like this:
functionclassFactory2<T>(){abstractclassACS{constructor(_y: number){}x!: T}returnACS}constACS=classFactory2<string>()// this doesn't work because the abstract has no constructor (expected I guess)typeAC3=InstanceType<typeofACS>["x"]// Type 'typeof ACS' does not satisfy the constraint 'new (...args: any) => any'typeAC4=ConstructorParameters<typeofACS>// Type 'typeof ACS' does not satisfy the constraint 'new (...args: any) => any'.// but the intersection of the type with anything makes it somehow work... ?// as if the intersection made the class get "unabstracted"typeAC1=InstanceType<(typeofACS)&{whatever: any}>["x"]// weirdly enough this gives stringtypeAC2=ConstructorParameters<(typeofACS)&{whatever: any}>// weirdly enough this gives [number]
Activity
xaviergonz commentedon Dec 9, 2019
I have to add that if in classFactory2 the return statement is changed to
return ACS as typeof ACS & {prototype: typeof ACS }Then oddly enough the "InstanceViaNew" works, yet "InstanceViaProto" still doesn't (I'd have expected the opposite)
RyanCavanaugh commentedon Jan 22, 2020
InstanceViaProtoought to work here; we're probably failing to apply the type mapper in the right place?xaviergonz commentedon Jan 24, 2020
Ron Buckton (@rbuckton) I think the PR will certainly make InstanceViaNew work (nice!), but I guess it wouldn't fix the InstanceViaProto case?
xaviergonz commentedon Jan 24, 2020
Also I found out that if you do something like this: