Skip to content

feature request: support for mixins composed from other mixins. #32080

Description

@trusktr

Search Terms

Suggestion

At the moment, it seems to be very difficult to compose mixins from other mixins.

Here's an example on StackOverflow: https://stackoverflow.com/questions/56680049

Here's an example on playground.

The code:

typeConstructor<T=any,Aextendsany[]=any[]>=new(...a: A)=>TfunctionFooMixin<TextendsConstructor>(Base: T){returnclassFooextendsBase{foo='foo'}}functionBarMixin<TextendsConstructor>(Base: T){returnclassBarextendsFooMixin(Base){test(){console.log(this.foo)// PROBLEM: this.foo is 'any' =(}}}

Use Cases

To make it simpler to make mixins (and compose them) like we can in plain JavaScript.

I'm porting JavaScript code to TypeScript, and the JavaScript makes great use of mixins (including composing new mixins from other mixins), but the composition ispractically impossible to do in TypeScript without very tedious type casting.

Examples

Here is the plain JS version of the above example:

functionFooMixin(Base){returnclassFooextendsBase{foo='foo'}}functionBarMixin(Base){// BarMixin is composed with FooMixinreturnclassBarextendsFooMixin(Base){test(){console.log(this.foo)// this.foo is obviously inherited from FooMixin!// ^--- This shoud not be an error!}}}

It seems to me, that the type checker can realize that the class returned from FooMixin(Base) will be a typeof Foo. The type system could at least be able to allow the Bar class to use methods and properties from Foo, despite not knowing what the Base class will be.

You can also imagine this problem gets worse with more composition, f.e.

returnclassBarextendsFoo(Baz(Lorem(Ipsum(Base)))){

It should also be possible to constrain the constructor to inherit from a certain base class. For example, the following doesn't work:

(EDIT: this part may actually be moved to a separate issue)
(EDIT 2: this part seems to be resolved)

// Think about Custom Elements here:functionFooMixin<TextendstypeofHTMLElement>(Base: T){returnclassFooextendsBase{test(){this.setAttribute('foo','bar')}}}

playground link

As Titian Cernicova-Dragomir (@dragomirtitian) pointed out on SO, there are workarounds, but they appear to be very complicated and impractical.

Here's a more realistic example of what I'm doing in JS (and trying to port to TS): I'm using a Mixin() helper function, as a type declaration for the following example, which in practice implements things like Symbol.hasInstance to check if instances are instanceof a given mixin, prevents duplicate mixin applications, and other features, but the types don't work:

typeConstructor<T=any,Aextendsany[]=any[]>=new(...a: A)=>TtypeMixinFunction=<TSuper>(baseClass: Constructor<TSuper>)=>Constructor<TSuper>// this function does awesome: ensures mixins aren't applied// more than once on a prototype chain, sets up Symbol.hasInstance so that// instanceof checks works with any mixin application, etc.declarefunctionMixin<TextendsMixinFunction>(mixinFn: T,DefaultBase?: Constructor): ReturnType<T>&{mixin: T}functionFooMixin<TextendsConstructor>(Base: T){returnclassFooextendsBase{foo='foo'}}constFoo=Mixin(FooMixin)typeFoo=typeofFoofunctionBarMixin<TextendsConstructor>(Base: T){returnclassBarextendsFoo.mixin(Base){bar='bar'test(){this.foo='foofoo'// should work!}}}constBar=Mixin(BarMixin)classBazextendsBar{test(){this.bar='barbar'// should work!this.foo='foofoo'// should work!}}constf: Foo=newBar()

playground link

Is there a way to do this currently, that we may have missed? (cc: Justin Fagnani (@justinfagnani))

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions