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
Sorry for the crap title, I'm not entirely sure what is going on but I think it's a bug. I also think this is somewhat related to #5449 but am unsure.
Library Example:
exportinterfaceInstanceConstructor<TextendsBaseModel>{new(fac: Factory<T>): T;}exportclassFactory<TextendsBaseModel>{constructor(privatecls: InstanceConstructor<T>){}get(){returnnewthis.cls(this);}}exportclassBaseModel{constructor(privatefac: Factory<this>){}refresh(){// get returns a new instance, but it should be of// type Model, not BaseModel.returnthis.fac.get();}}
User Code Example:
exportclassModelextendsBaseModel{do(){returntrue;}}// Kinda sucks that Factory cannot infer the "Model" typeletf=newFactory<Model>(Model);leta=f.get();letb=a.refresh();
The issue that the compiler complains about is as follows:
error TS2345: Argument of type 'typeof Model' is not assignable to parameter of type 'InstanceConstructor<Model>'.
Types of parameters 'api' and 'api' are incompatible.
Type 'Factory<this>' is not assignable to type 'Factory<Model>'.
Type 'this' is not assignable to type 'Model'.
Type 'BaseModel' is not assignable to type 'Model'.
Property 'do' is missing in type 'BaseModel'.
If I change the extended model to be specific it errors in different way, but same theme.
error TS2345: Argument of type 'Factory<Model>' is not assignable to parameter of type 'Factory<this>'.
Type 'Model' is not assignable to type 'this'.
I think the reason is because this isn't being polymorphic in either the template or the constructor. When this is analyzed in the constructor of BaseModel it seems to stick with that context.
this looks like a duplicate of #5449, this types are not supported in constructor arguments, nor is it supported in static methods parameters or return types. #5449 tracks adding the correct error message.
So this use case is not supported? Based on my example is there any other way to accomplish what I'm attempting? This isn't an unusual pattern for Resource/Model implementation.
Fortunately the type system still seems to inspect and output things correctly so this isn't slowing me down much which is great. I'll keep an eye on these two issues for updates.
Sorry for the crap title, I'm not entirely sure what is going on but I think it's a bug. I also think this is somewhat related to #5449 but am unsure.
Library Example:
User Code Example:
The issue that the compiler complains about is as follows:
If I change the extended model to be specific it errors in different way, but same theme.
I think the reason is because
this
isn't being polymorphic in either the template or the constructor. Whenthis
is analyzed in the constructor ofBaseModel
it seems to stick with that context.I initially posted about the use of polymorphic
this
which was answered by @RyanCavanaugh http://stackoverflow.com/questions/33443793/create-a-generic-factory-in-typescriptThe text was updated successfully, but these errors were encountered: