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

Recursive definitions in mixins #29872

SamuraiJack opened this Issue Feb 12, 2019 · 0 comments


None yet
2 participants
Copy link

SamuraiJack commented Feb 12, 2019

Its currently possible to create a circular references in classes just fine:

export class Class1 {
    another     : Class2     // compiles fine
export class Class2 {
    another     : Class1    // compiles fine

However, the same thing fails when using mixin-based classes:

export const SampleMixin1 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin1 extends base {
    another             : SampleMixin2 // TS2502: 'another' is referenced directly or indirectly in its own type annotation
export type SampleMixin1 = Mixin<typeof SampleMixin1>

export const SampleMixin2 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin2 extends base {
    another             : SampleMixin1 // TS2502: 'another' is referenced directly or indirectly in its own type annotation
export type SampleMixin2 = Mixin<typeof SampleMixin2>

// supporting declarations for mixin pattern
export type AnyFunction<A = any>      = (...input: any[]) => A
export type AnyConstructor<A = any>   = new (...input: any[]) => A
export type Mixin<T extends AnyFunction> = InstanceType<ReturnType<T>>

This makes things much more complicated, you need to introduce some dummy interfaces, etc, etc.

The workaround exists - to use different form of creating the type for the standalone mixin class - with interfaces:

export const SampleMixin3 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin3 extends base {
    another             : SampleMixin4
export interface SampleMixin3 extends Mixin<typeof SampleMixin3> {}

export const SampleMixin4 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin4 extends base {
    another             : SampleMixin3
export interface SampleMixin4 extends Mixin<typeof SampleMixin4> {}

But this notation seems to drive crazy the IDE (the language server under the hood?). It stop finding usages of properties, show many false type errors etc. Basically in such approach you are limited to old-good console npx tsc launch (which works well at least), but all the development aid facilities don't work.

I think mixin pattern should be supported first class. I'd even create some language construct for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.