Repository navigation
Description
Search Terms
Suggestion
Based on https://stackoverflow.com/questions/56680049, it seems mixin classes can't be used inside other mixins.
It'd be great if, in that example, the outer mixin would be able to successfully use the props and types from the Sizeable mixin.
Note, in the example, Sizeable is a class with a default Base class applied, and .mixin is the mixin function used to create it.
Basically, the following (in JS, and simplified):
function SizeableMixin(Base = class {}) {
return class Sizeable extends Base {...}
}
const Sizeable = SizeableMixin()
Sizeable.mixin = SizeableMixin
export default SizeableSo it's just a regular class-factory mixin (with a default application so that it is easy to extend like a normal class when used as a base class).
Use Cases
To be able to use Mixins anywhere, including inside other mixins.
Examples
import Sizeable from './Sizeable'
function FooMixin<T extends Constructor>(Base: T) {
const Parent = Sizeable.mixin(Base)
return class Foo extends Parent { ... } // ERROR, [Parent] is not a constructor function.
}
const Foo = FooMixin()
Foo.mixin = FooMixin // ignore types here, for now
export default FooBut actually, Parent is a constructor. We know it is. It'd be great to use it.
In the Foo mixin, I should at least be able to correctly use Sizeable properties and methods, as if Sizeable were the base class.
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.
Activity
trusktr commentedon Jul 23, 2019
Ryan Cavanaugh (@RyanCavanaugh) I've been working away the problems using
Constructorhelpers with explicitas unknown as Constructor<Instance> & Staticcasts, usingConstructor.Here's an example that shows the use of a custom
Constructorhelper to work around the problems:playground
The example shows:
Constructorhelper for that).Tin factory generic params)trusktr commentedon Jul 23, 2019
I updated the above example, to show the full pattern I use (needed?) to have proper mixin classes.
trusktr commentedon Jul 23, 2019
Oh, right, so back to the main point in the OP: The pattern I just illustrated now makes it possible to compose mixins out of other mixins in an understandable way through use of the
Constructorhelper:playground
So, to point out the main issue of the OP again, one would think it would be sufficient to write:
but that results in the error
playground
which makes all code that uses
AnotherMixinto break.We know
Baseis aConstructor, and we're telling TypeScript that it is, by forcefully casting it.Would it be possible for TypeScript to just know?
trusktr commentedon Dec 28, 2019
Closing as duplicate of #32080