Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for class-factory mixins. #9236

Closed
trusktr opened this issue Dec 6, 2024 · 1 comment
Closed

Support for class-factory mixins. #9236

trusktr opened this issue Dec 6, 2024 · 1 comment

Comments

@trusktr
Copy link
Contributor

trusktr commented Dec 6, 2024

Proposal

Can you please support class factory mixins?

They are incredibly easy, and powerful, in plain JavaScript. I encounter people from plain JS coming to TypeScript (in which class-factory mixins are possible) complaining that it is mind-boggling difficult to implement them with TypeScript.

In Flow, it is next to impossible, making this wonderful feature from JavaScript not usable in Flow.

Use case

The concept in JavaScript in incredibly simple:

function FooMixin(Base) {
  return class Foo extends Base {
    fooMethod(): number { return 123 }
  }
}

and then use it, for example convert this code,

import {BaseClass} from 'somewhere'

export class MyClass extends BaseClass {
  myMethod(): boolean { return true }
}

const o = new MyClass()

const b: boolean = o.myMethod()

to this:

import {BaseClass} from 'somewhere'
import {FooMixin} from 'foo-lib'

export class MyClass extends FooMixin(BaseClass) {
  myMethod(): boolean { return true }
}

const o = new MyClass()

const b: boolean = o.myMethod()
const n: number = o.fooMethod() // new method from FooMixin

This is incredibly useful for composition in plain JavaScript, yet in Flow it is next to impossible, and in TypeScript it is doable but still too complicated.

We need to be able to easily express plain JavaScript concepts. It is super clear to the human mind what the mixin does in plain JS.

I don't have time to implement this in Flow myself, but I hope someone can please seriously consider it because this feature of plain JavaScript is very useful for code re-use.

@SamChou19815
Copy link
Contributor

Given all the pain of typing React HOC in the past, we know that typing things like this will be verbose at least. We do not intend to support this, because we don't have a goal to make all the code that runs fine type check and make you type the least number of characters.

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants