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
The anonymous class is overriding the fooMethod() with an incompatible signature, but there is nothing in the signature of create() that should prohibit Foo from filling T. You would need to somehow express in the signature of create() that T must either not have a fooMethod or has a fooMethod with a signature that (x:string):void is compatible with (i.e. the override will be compatible).
Aleksey-Bykov, I completely agree that we need something like that. However Jesse Schalken (@jesseschalken) is also right. I'd propose other solution, that can look way to far from your solution (class constraint) but will help to solve the same problem.
I would propose the special type constructor: T overrides U
It is not a constraint, it is a type. So you freely can write:
vara: {x: number}overrides{x: {},y: boolean};// and get the correct { x: number, y: boolean}
As well as
varX: TExtensionoverridesTBase;// where both TExtension and TBase are generic type params
This operator ensures that extension is correct at the instantiation time.
vara: {x: number}overrides{x: string,y: boolean};// error: number and string are not compatible
exportfunctioncreate<T>(base: new()=>T}): {fooMethod(x: string): void}overridesT{returnnewclassextendsbase{publicfooMethod(x:string):void{}};}classFoo{publicfooMethod(x:number):string{return'hello';}}create<Foo>(Foo);// error at compile time
Activity
jesseschalken commentedon Jul 17, 2016
How would TS check that the anonymous class is compatible with
base, whenbaseis determined at run time?Eg
The anonymous class is overriding the
fooMethod()with an incompatible signature, but there is nothing in the signature ofcreate()that should prohibitFoofrom fillingT. You would need to somehow express in the signature ofcreate()thatTmust either not have afooMethodor has afooMethodwith a signature that(x:string):voidis compatible with (i.e. the override will be compatible).zpdDG4gta8XKpMCd commentedon Jul 17, 2016
A good question, it is certainly possible:
createwhen the type parameter T gets resolved to a real typecreateby constrainingT unlike CwhereCis the type of the anmonitors class: negating type constraints #7993Artazor commentedon Jul 17, 2016
Seems that Anders Hejlsberg (@ahejlsberg) already shown some workaround for the problem described by Aleksey-Bykov, could not find, though.
Aleksey-Bykov, I completely agree that we need something like that. However Jesse Schalken (@jesseschalken) is also right. I'd propose other solution, that can look way to far from your solution (class constraint) but will help to solve the same problem.
I would propose the special type constructor:
T overrides UIt is not a constraint, it is a type. So you freely can write:
As well as
This operator ensures that extension is correct at the instantiation time.
Guys, what do you think?
Artazor commentedon Jul 17, 2016
in my case, Jesse Schalken (@jesseschalken) example would look like:
Artazor commentedon Jul 17, 2016
For all actual types
TandUthe typeT overrides Uinferred exactly according to the TypeScript member overriding rules.ping Ryan Cavanaugh (@RyanCavanaugh)
zpdDG4gta8XKpMCd commentedon Jul 17, 2016
I like
overridesmhegazy commentedon Jul 17, 2016
looks like a duplicate of #4890
Artazor commentedon Jul 17, 2016
Mohamed Hegazy (@mhegazy) - exactly, it's that duplicate I've tried to find. Aleksey-Bykov let's go there and move the process forward -)