microsoft / microsoft/TypeScript

Mixing base class defeats circularity protection

未关闭
#61,721 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Domain: check: Type Circularity Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔎 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

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEkoGdnwEIpAYQPYFsAjKAFygDsT4BvAKHkV3ORJgFcwTcYAKAOgGwA5sgBc8ANp4ipCiQkAiAA4xcShQF0NASgDc9eAHpDDU2fNmAetZu271+AHJhyR-ACWaOADMQccmAgwPDA7nCcEACe8Nwe5KHhJFFxHiRouADu5PAkkUoIFOS4ZCTuTHy0AL60tKBIcIjQqPAAkpR+5FAQ2BDuIJQAIrhgbPj9VHQMYEws7Jzc-IIwIuIUkRI6+tW14NAN3mwBpUzwPX2Dw6PjALLuAB7u5AA8mMg4TcgAfDwGryDifx6KGQtG04ly+Vw3la7RgnW6vXGQxGY0o8AAZBgsEDUPpjPBLPAAEogfC4ABuCAABhCQFCYSQOl0zkjLqiSFT4BRgiQABYIMBhEZ7dy5eBCXAgNBQDJQSIVHb1AUfU4EYhkNEgO6M+JoFkXFE3e6PHiAtWySjaGgGfEWcz2QkAFTyCEc0nVcjc4TYMGQ7kpyR8fn6gTQoreEGhKC58GIbxyLoqDFtdtMDqc7otJDcnngQf8gWCCXASWisUexYi0UeqXSWVjWHgWpUUr95QMKjU4nIo0Ifi2NSAA

💻 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 TypeScript Playground 复现开始,跟踪针对计算得到的 mixin 基础表达式及其交集返回类型的循环性诊断。完成标准是,所示的 mixin 模式(包括有用的非空 mixin 情况)不会产生错误的递归基础或自引用注释错误。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。