microsoft / microsoft/TypeScript
Mixing base class defeats circularity protection
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 Search Terms
mixin, referenced directly or indirectly in its own type annotation, recursively references itself as a base type, referenced directly or indirectly in its own base expression.
🕗 Version & Regression Information
- This changed in the PR #39675
Given that this was when base types were consistently checked for errors this has effectively existed forever, just only started being reported in #39675.
⏯ Playground Link
💻 Code
declare class BaseCombatant {
constructor(...args: [Combatant["prop"]]);
// ^^^^^^^^^^^^^^^^^ 'args' is referenced directly or indirectly in its own type annotation.
}
declare class InternalClientDocument {
constructor(...args: any[]);
}
declare function ClientDocumentMixin<BaseClass>(
Base: BaseClass
): typeof InternalClientDocument & BaseClass;
// ^ Remove `typeof InternalClientDocument` and the circularity goes away.
declare class Combatant extends ClientDocumentMixin(BaseCombatant) {
// ^^^^^^^ Type 'Combatant' recursively references itself as a base type.
// ^^^^^^^ 'Combatant' is referenced directly or indirectly in its own base expression.
prop: number;
}
🙁 Actual behavior
Numerous errors.
🙂 Expected behavior
No error. This pattern is not intractable, in fact if you remove the useless typeof InternalClientDocument in typeof InternalClientDocument & BaseClass it works.
In reality I'm actually dealing with a useful mixin, not an empty class, so I would prefer this to work for a complete mixin as well. I'm guessing the issue has something to do with weaker caching around arbitrary expressions as the base class compared to the typical base class with a concrete name.
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and trace circularity diagnostics for the computed mixin base expression and its intersection return type. Done means the shown mixin pattern, including the useful non-empty mixin case, produces no erroneous recursive-base or self-referential-annotation errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100