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
Can't extend mixin constructor that constructs a generic type #13807
This applies regardless of whether or not I intersect it with some bogus type, or have a reasonable constraint. So none of the following works.
Returning a plain T
exporttypeConstructor<T>=new(...args: any[])=>TexportinterfaceTimestamp{timestamp: Date;}// plain old TexportfunctionTimestamp<T,CTextendsConstructor<T>>(Base: CT): Constructor<Timestamp>&Constructor<T>{returnclassextendsBase{timestamp=newDate();}}
Constructor of T with a bogus object type ({})
exporttypeConstructor<T>=new(...args: any[])=>TexportinterfaceTimestamp{timestamp: Date;}// Constructor of T with a bogus object typeexportfunctionTimestamp<T,CTextendsConstructor<T&{}>>(Base: CT): Constructor<Timestamp>&Constructor<T>{returnclassextendsBase{timestamp=newDate();}}
Mohamed Hegazy (@mhegazy) mentioned offline that T is useless in these cases anyway. I think that's beside the point, because you can imagine that when actually using the mixin pattern, you will utilize T in some fashion.
Is there a real world scenario for this? Just trying to understand what you want to accomplish. We would obviously need to ensure T is constrained to an object-like type, but otherwise we could probably do this if we think it is worth the added complexity.
throws the error any is not a constructor function type, even though none of those variables or types are any.
If I try specifying the argument Test, e.g. withMixin<...>(Test<T>), it calls withMixin with the wrong arguments - withMixin(Test(), {}) - which in turn causes an Test cannot be called without new runtime error`.
I'm trying to do something similar where I want to use a class-mixin in another class-mixin.
A small example here:
classMyClass{myClass=1;}typeConstructor<TextendsMyClass>=new(...args: any[])=>T;functionmake1<TextendsConstructor<MyClass>>(base: T){returnclassextendsbase{make1=1;};}/** * [ts] Type '{ * new (...args: any[]): make3<T>.(Anonymous class); * prototype: make3<any>.(Anonymous class); } & T' * is not a constructor function type. [2507] */functionmake2<TextendsConstructor<MyClass>>(base: T){returnclassextendsmake1(base){make2=2;};}classTestextendsMyClass{}classTest2extendsmake2(Test){constructor(){super();// this.myClass is gone// this.make1 is gone// only this.test2 exists}}
Activity
DanielRosenwasser commentedon Feb 1, 2017
Mohamed Hegazy (@mhegazy) mentioned offline that
Tis useless in these cases anyway. I think that's beside the point, because you can imagine that when actually using the mixin pattern, you will utilizeTin some fashion.ahejlsberg commentedon Feb 1, 2017
Is there a real world scenario for this? Just trying to understand what you want to accomplish. We would obviously need to ensure
Tis constrained to an object-like type, but otherwise we could probably do this if we think it is worth the added complexity.appsforartists commentedon Apr 4, 2017
Anders Hejlsberg (@ahejlsberg), here's a real commit where I'm trying to combine mixins and generics:
http://codereview.cc/D3000
throws the error
any is not a constructor function type,even though none of those variables or types areany.If I try specifying the argument
Test, e.g.withMixin<...>(Test<T>), it callswithMixinwith the wrong arguments -withMixin(Test(), {})- which in turn causes anTest cannot be called without newruntime error`.appsforartists commentedon Apr 4, 2017
(I've updated that diff to include the workaround from #14017 (comment). To see what I had before, click here.)
[added] withNextOperator to /operators/foundation
alber70g commentedon Jan 28, 2019
Any resolution on this?
I'm trying to do something similar where I want to use a class-mixin in another class-mixin.
A small example here:
alber70g commentedon Feb 4, 2019
Anything to say about this? Is there a workaround possible? I'm anxious to hear about this, since we need it.
Another option could be that this is not proper coding, then I'd like to hear about it, as well.