Following the great mixin implementation by Anders Hejlsberg (@ahejlsberg) (#13743)
Having a mixin function returns a class is powerful but requires manual type declaration.
interfaceTagged{_tag: string;}classPoint{constructor(publicx: number,publicy: number){}}typeConstructor<T>=new(...args: any[])=>T;functionTagged<TextendsConstructor<{}>>(Base: T){returnclassextendsBase{_tag: string;constructor(...args: any[]){super(...args);this._tag="";}}}exportconstTaggedPoint: Constructor<Tagged>&typeofPoint=Tagged(Point);consttPoint: TaggedPoint;// ERROR: Cannot find name 'TaggedPoint'We can't use TaggedPoint as a type, unless we:
exporttypeTaggedPoint=Constructor<Tagged>&typeofPoint;consttPoint: TaggedPoint;// NOW OK
Using classes we can
exportclassCustomerextendsTagged(Person){accountBalance: number;}Which exposes the type but forces the developer to extend the Customer class.
My feature request is allowing:
exportclassTaggedPoint=Tagged(Point);
Which will be just sugar for:
exportconstTaggedPoint=Tagged(Point);exporttypeTaggedPoint=Constructor<Tagged>&typeofPoint;
Maybe even:
exportclassTaggedPoint=Tagged(Point){// user can extend here...}but that's not the main point...
The motivation for this is a more robust mixin mechanism that revolves around the base class and allow mixin's to know about it, using generic it provides powerful combinations.
So I can have
RestMixin(Base,Mixin1,Mixin2);
And the return type can have members from Mixin1 knowning about Base, using interfaces one can even set static members of a mixin to return T which is the instance of RestMixin(Base, Mixin1, Mixin2) all together.
Following the great mixin implementation by Anders Hejlsberg (@ahejlsberg) (#13743)
Having a mixin function returns a class is powerful but requires manual type declaration.
We can't use
TaggedPointas a type, unless we:Using classes we can
Which exposes the type but forces the developer to
extendtheCustomerclass.My feature request is allowing:
Which will be just sugar for:
Maybe even:
but that's not the main point...
The motivation for this is a more robust mixin mechanism that revolves around the base class and allow mixin's to know about it, using generic it provides powerful combinations.
So I can have
And the return type can have members from Mixin1 knowning about Base, using interfaces one can even set static members of a mixin to return T which is the instance of
RestMixin(Base, Mixin1, Mixin2)all together.