microsoft / microsoft/TypeScript
Typescript has more trouble resolving circular class references when using mixins / through function invocations
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
🔎 Search Terms
Mixin function invocation circular inherits references itself
Related issues:
https://github.com/microsoft/TypeScript/issues/29872
https://github.com/microsoft/TypeScript/issues/42383
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about limit of typescript and resolving circular references.
⏯ Playground Link
💻 Code
type ConstructorType<T> = abstract new (...args: any[]) => T;
function makeMixin<TBase extends ConstructorType<any>, FinalConstructor>(
mixinFunction: (Base: TBase) => FinalConstructor
): (Base?: TBase) => FinalConstructor {
return (Base?: TBase) => {
// do other things with Base, this example is simplified
return mixinFunction(Base ?? (Object as any));
};
}
const MixinF = makeMixin(<TBase extends ConstructorType<any>>(Base: TBase) => {
abstract class Mixin extends Base {
foo(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
return Mixin
})
const Mixin2F = makeMixin(<TBase extends ConstructorType<any>>(Base: TBase) => {
abstract class Mixin2 extends Base {
bar(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
return Mixin2
})
class MixinApplied extends Mixin2F(MixinF()) {}
🙁 Actual behavior
The declaration for either Mixin1F or Mixin2F (unclear why it's not deterministic) errors with "'Mixin2F' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer." and the declaration for MixinApplied errors with "Type 'MixinApplied' recursively references itself as a base type.(2310)
'MixinApplied' is referenced directly or indirectly in its own base expression.(2506)"
I imagine I'm just running into some limits of the TypeScript compiler, but I can come up with at least 3 different scenarios that end with the same final result but without TypeScript errors, and this is preventing perfectly fine Javascript from being written. Mixins and function invocations seem to exacerbate the problem of recursive / circular references in class definitions.
For example, calling makeMixin within the function (instead of wrapping the function) and immediately invoking it works (TS playground):
type ConstructorType<T> = abstract new (...args: any[]) => T;
function makeMixin<TBase extends ConstructorType<any>, FinalConstructor>(
mixinFunction: (Base: TBase) => FinalConstructor
): (Base?: TBase) => FinalConstructor {
return (Base?: TBase) => {
return mixinFunction(Base ?? (Object as any));
};
}
const MixinF = <TBase extends ConstructorType<any>>(Base?: TBase) => {
return makeMixin((Base: TBase) => {
abstract class Mixin extends Base {
foo(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
return Mixin
})(Base)
}
const Mixin2F = <TBase extends ConstructorType<any>>(Base?: TBase) => {
return makeMixin((Base: TBase) => {
abstract class Mixin2 extends Base {
bar(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
return Mixin2
})(Base)
}
class MixinApplied extends Mixin2F(MixinF(Object)) {} // note: If I don't supply Object here, this fails for a different reason
const x = {} as MixinApplied;
x.foo(x, true);
x.bar(x, false);
Removing the makeMixin call also works (TS playground):
type ConstructorType<T> = abstract new (...args: any[]) => T;
const MixinF = <TBase extends ConstructorType<any>>(Base: TBase) => {
abstract class Mixin extends Base {
foo(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
return Mixin
}
const Mixin2F = <TBase extends ConstructorType<any>>(Base: TBase) => {
abstract class Mixin2 extends Base {
bar(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
return Mixin2
}
class MixinApplied extends Mixin2F(MixinF(Object)) {}
And of course the non-mixin version works as well (TS playground):
abstract class Mixin extends Object {
foo(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
abstract class Mixin2 extends Mixin {
bar(foo: MixinApplied, extra: boolean): MixinApplied | boolean { return extra }
}
class MixinApplied extends Mixin2 {}
🙂 Expected behavior
It'd be great if the mixin + function invocation example worked as well as the others.
Additional information about the issue
No response
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par exécuter les reproductions liées de TypeScript Playground et par comparer la version défaillante avec mixin/appel de fonction aux variantes fonctionnelles. Lisez les issues associées 29872 et 42383 pour connaître le contexte antérieur. Le travail est considéré comme terminé lorsque l’exemple de mixin signalé est vérifié par le système de types sans erreurs de références circulaires et que les exemples fonctionnels existants restent valides.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 30/100