Repository: TypeScript
Issue: feature request: support for mixins composed from other mixins.
Labels: Suggestion,Awaiting More Feedback

## 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](https://www.typescriptlang.org/play/#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqUAvFAIYIgA0UAglBAB7AQIAmS5lA2gLolcQfAHwCEEAO5QAFADp5ZAFx0AlCVEEAsACgdAM3QJsAS0RQAYnDgBZY42MJCDZqw6xEKDNlzDpAITIkCGUCNQBvKB0dKBioVAhgdFQEKEwAG0DOSzhnFnZOAKCoCOjYsr0rAQByCrgq0piAXyjtZt1tAyNgUxSA1Ft7RyImPLd4ZDQsHFRfQuCoUOLI9rL4xOTUjKQCslRc1yyrAYd-QIhw5bKylhRpC4arsswPODSIWTS4AHNpYAALYxIWS1NQAelBMQAEgBRABK0Jo-0BwMqgKgVQoICqJGkDzKbXxLUaQA).

The code:

```ts
type Constructor<T = any, A extends any[] = any[]> = new (...a: A) => T

function FooMixin<T extends Constructor>(Base: T) { 
    return class Foo extends Base { 
        foo = 'foo'
    }
}

function BarMixin<T extends Constructor>(Base: T) { 
    return class Bar extends FooMixin(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:

```js
function FooMixin(Base) { 
    return class Foo extends Base { 
        foo = 'foo'
    }
}

function BarMixin(Base) { 
    // BarMixin is composed with FooMixin
    return class Bar extends FooMixin(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.

```js
    return class Bar extends Foo(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)

```ts
// Think about Custom Elements here:
function FooMixin<T extends typeof HTMLElement>(Base: T) { 
    return class Foo extends Base { 
        test() {
            this.setAttribute('foo', 'bar')
        }
    }
}
```

[playground link](http://www.typescriptlang.org/play/#src=function%20FooMixin%3CT%20extends%20typeof%20HTMLElement%3E(Base%3A%20T)%20%7B%20%0D%0A%0D%0A%20%20%20%20return%20class%20Foo%20extends%20Base%20%7B%20%0D%0A%20%20%20%20%20%20%20%20test()%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20this.setAttribute('foo'%2C%20'bar')%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%7D)

As @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:

```ts
type Constructor<T = any, A extends any[] = any[]> = new (...a: A) => T

type MixinFunction = <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.
declare function Mixin<T extends MixinFunction>(
    mixinFn: T,
    DefaultBase?: Constructor
): ReturnType<T> & {mixin: T}

function FooMixin<T extends Constructor>(Base: T) { 
    return class Foo extends Base { 
        foo = 'foo'
    }
}

const Foo = Mixin(FooMixin)
type Foo = typeof Foo


function BarMixin<T extends Constructor>(Base: T) { 
    return class Bar extends Foo.mixin(Base) {
        bar = 'bar'

        test() {
            this.foo = 'foofoo' // should work!
        }
    }
}

const Bar = Mixin(BarMixin)

class Baz extends Bar {

    test() {
        this.bar = 'barbar' // should work!
        this.foo = 'foofoo' // should work!
    }

}

const f: Foo = new Bar()
```

[playground link](http://www.typescriptlang.org/play/#src=type%20Constructor%3CT%20%3D%20any%2C%20A%20extends%20any%5B%5D%20%3D%20any%5B%5D%3E%20%3D%20new%20(...a%3A%20A)%20%3D%3E%20T%0D%0A%0D%0Atype%20MixinFunction%20%3D%20%3CTSuper%3E(baseClass%3A%20Constructor%3CTSuper%3E)%20%3D%3E%20Constructor%3CTSuper%3E%0D%0A%0D%0A%2F%2F%20this%20function%20does%20awesome%3A%20ensures%20mixins%20aren't%20applied%0D%0A%2F%2F%20more%20than%20once%20on%20a%20prototype%20chain%2C%20sets%20up%20Symbol.hasInstance%20so%20that%0D%0A%2F%2F%20instanceof%20checks%20works%20with%20any%20mixin%20application%2C%20etc.%0D%0Adeclare%20function%20Mixin%3CT%20extends%20MixinFunction%3E(%0D%0A%20%20%20%20mixinFn%3A%20T%2C%0D%0A%20%20%20%20DefaultBase%3F%3A%20Constructor%0D%0A)%3A%20ReturnType%3CT%3E%20%26%20%7Bmixin%3A%20T%7D%0D%0A%0D%0Afunction%20FooMixin%3CT%20extends%20Constructor%3E(Base%3A%20T)%20%7B%20%0D%0A%20%20%20%20return%20class%20Foo%20extends%20Base%20%7B%20%0D%0A%20%20%20%20%20%20%20%20foo%20%3D%20'foo'%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Foo%20%3D%20Mixin(FooMixin)%0D%0Atype%20Foo%20%3D%20typeof%20Foo%0D%0A%0D%0A%0D%0Afunction%20BarMixin%3CT%20extends%20Constructor%3E(Base%3A%20T)%20%7B%20%0D%0A%20%20%20%20return%20class%20Bar%20extends%20Foo.mixin(Base)%20%7B%0D%0A%20%20%20%20%20%20%20%20bar%20%3D%20'bar'%0D%0A%0D%0A%20%20%20%20%20%20%20%20test()%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20this.foo%20%3D%20'foofoo'%20%2F%2F%20should%20work!%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Bar%20%3D%20Mixin(BarMixin)%0D%0A%0D%0Aclass%20Baz%20extends%20Bar%20%7B%0D%0A%0D%0A%20%20%20%20test()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.bar%20%3D%20'barbar'%20%2F%2F%20should%20work!%0D%0A%20%20%20%20%20%20%20%20this.foo%20%3D%20'foofoo'%20%2F%2F%20should%20work!%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%7D%0D%0A%0D%0Aconst%20f%3A%20Foo%20%3D%20new%20Bar())

Is there a way to do this currently, that we may have missed? (cc: @justinfagnani)

## Checklist

My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).