Recursive definitions in mixins
See original GitHub issueIts 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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:15
- Comments:8 (1 by maintainers)
Top GitHub Comments
Any hope for a proper solution of this issue?
@trusktr Can you try making it working as in my 2nd example here? Basically, create a
SampleMixin3
which extendsSampleMixin1
andSampleMixin4
, which extendsSampleMixin2
. Then make a cycle between them