Skip to content

Feature request: Support for using mixins inside other mixins. #32004

Closed
@trusktr

Description

@trusktr

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 Sizeable

So 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 Foo

But 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.

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