tannerntannern / ts-mixer

A small TypeScript library that provides tolerable Mixin functionality.
MIT License
379 stars 27 forks source link

Subclasses that are meant to be mixed in #68

Closed gotjoshua closed 7 months ago

gotjoshua commented 7 months ago

image

I am trying to use props and methods from the base and or other mixed in classes ... I would love to have intellisense working.

adding this prop to the classes that know they will be mixed is the least horrible (but admittadly ugly) way i found so far.

protected mixed = (this as unknown as FooBar)
inBar = this.mixed.fromFoo

any other magic suggestions?

tannerntannern commented 7 months ago

Can you step back and describe exactly what you're trying to accomplish?

gotjoshua commented 7 months ago

Sure, sorry if it wasn't clear. I want to mix multiple classes, and to be able to use methods and attributes from the other "sub"classes and find them on "this" via intellisense.

I'm using Mixin to enable separating different parts of a class that grew too huge, but it has many interconnected parts.

The only other thought i had was to create a kind of mixin dependency tree, so that there is a base class that has the commonly needed stuff, and that is extended instead of mixed in, but i'm looking for other possibilities!

Thanks for your reply and for offering the lib!

tannerntannern commented 7 months ago

I think the issue (in the example you posted at least) is that you're trying to have your mixed classes be aware of what they're going to be mixed with ahead of time, which is not possible with mixins or any other form of inheritance. You can force the type system to partially do what you want, but what you're doing isn't sound from a typing perspective. For example, you're going to have a runtime error if Foo is reused in another mixin that doesn't include FooBar or is even just instantiated on its own.

Mixins generally should be self-contained. If you find your mixins reaching across class boundaries, that could be a sign that those classes shouldn't be separated. You could try breaking the huge class into fewer pieces or reworking the components so they are each more isolated from each other.

Sorry I can't give more specific advice than that. I'm happy to help with other ts-mixer-specific issues, but I don't think this one is, so I'm closing it for now

gotjoshua commented 7 months ago

Mixins generally should be self-contained.

🙏