microsoft / microsoft/TypeScript

Resolution order dependent circular heritage

オープン
#61,711 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: check: Type Circularity Help Wanted Possible Improvement
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 Search Terms

resolution order dependent circular heritage, circular base type, circular mixin

🕗 Version & Regression Information
  • This changed in #39675

However, it appears this PR was the first time that circular heritage was (consistently) reported. Therefore this issue has essentially always existed.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/PTAEDEEsA8FMBNSQGagJ4HsCuoDGGBbA2AOwBdQyALSAZ1ABtITZRsKMAnUWgd0jK4qlDKAAGAQVxkuABQAqAJQBMsMQDoAUPFi4GAQ06t8JWhWgAuUAElTZfSVyx5aAA6wAPGTewMqKTKcCiqwAHwA3JpRIKAAqrQIIjxUGLyUVPocJAxooPqgOu4kOo65GCTi8mJI9CxOtLSGuchc6caQnLhYBpwCaFoxEvTMADRtoMjM+gygRrQYDABuiYvTWKzwGLC1GBQEmWSwnFre7qAAIrBF8LQA8iQe8qGgALx5JGiR2ro9xgYNoACrQA3ppQKBXJwMK4rJdrncHgFIMsAKLIZC6MgAbQARJDoTiALoRTQAXyiOj0hlYJH0xForn0ThshwIoFB4JiUDgiBQ6GwPH4gmE1DoSQcSHIR2QTNgWnBp1Y1gIrgYsGI5EykHKrxsdgcThc7g85wwXQ1ZAAwv9aJbypMAOa46ysokk8nfKlGPA2lnq9lg0CWC5XUg3e4eJGo9GY3H41xuyIerkwRJ8zA4IwEDDLcZiADKWAARka1KAHaQjpBcBDDHTYIdjpoqQCgUElKoPIWSz5dQ40M9YNBDsV6G32cmwNy06gM7N1TnWNRWAXi6XqhWWL0a4zOPXG1oW-QXergp3u6W+x9B8Ow8fWROosxGzLmabzaQrTa7SRHQHOVOqa8rOApCA4FbiIqfiAtIcgdmWMiQT40Ftmenj9qEYiBm2VhQf4sHtiEXwARAQFICBOBgSQEFiHhfoEGh1SIbRyGoCeDHwR4GFYeC7G4ax9FoUmFI-NSPr6K20jIrAaIYtIoBDiONygNakCfu+WAWgAsjAzAABQAEISbAUYyTG0gAJT-hCUIwqAJCaUWRzCZ6vwTFgjhkNqFSqepZqaZ+OnQMwHiBimPLkfyOAMBgGAJBUy54OUZh7s+bCoGIRkJNaEm0BogZZbAOUAopd72bAaR6eo1WGA6tBWP2WKElZLzPBgRYAFaYiMmihIZxlWIVxW0BZg3GcNXyUm5R6gIVpmyZi1kmClWAEVVNWcHVsKhqOEbseoyqquqn5auUoQWUmQA

💻 Code
// Fixed if you comment this line out or switch to `ActorPTR2e`.
declare const x: InstanceType<typeof ActorPTR2e>;


// Used to show that only a dependency on `T` is necessary for the circularity.
// As in, the final resolved value does not matter.
type DependsOn<T> = any;

declare class Actor {
  prop: DependsOn<ActiveEffect["prop"]>;
}

declare namespace Item {
  // Fixed if you switch this to an interface.
  type Implementation = InstanceType<DocumentClassConfig["Item"]>;
}

declare class Item {
  x: DependsOn<ActiveEffect["prop"]>;
}

// Fixed if you remove the `SubType` generic parameter.
class ActorPTR2e<SubType = any> extends Actor {}

// Fixed if you remove the `SubType` generic parameter.
class ItemPTR2e<SubType = any> extends Item {}

interface DocumentClassConfig {
  // Fixed if you change `typeof ActorPTR2e` to `typeof ActorPTR2e<any>`
  Actor: typeof ActorPTR2e;

  // Fixed if you change `typeof ItemPTR2e` to `typeof ItemPTR2e<any>`
  Item: typeof ItemPTR2e;
}

declare class ActiveEffect extends ClientDocumentMixin(BaseActiveEffect) {
  prop: number;
}

declare function ClientDocumentMixin<
  // Fixed if you loosen the constraint of `BaseClass`.
  BaseClass extends new (...args: any[]) => object,
>(Base: BaseClass): BaseClass;

declare class BaseActiveEffect {
  constructor(...args: DependsOn<Item.Implementation>);
}
🙁 Actual behavior

Numerous errors, namely:

Type alias 'Implementation' circularly references itself.
Type 'ActiveEffect' recursively references itself as a base type.
'ActiveEffect' is referenced directly or indirectly in its own base expression.
'args' is referenced directly or indirectly in its own type annotation.
🙂 Expected behavior

No error, as seen when you comment out the line declare const x: InstanceType<typeof ActorPTR2e>;. The error going away indicates that this circularity is resolution order dependent.

I understand how odd the code is out of context but I did actually run into this in a real codebase.

Additional information about the issue

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされている TypeScript Playground の再現例から始め、InstanceType、ジェネリッククラスの継承、mixin の制約に関する、報告された循環型エイリアス、基底型、アノテーションの診断を追跡します。再現例が、循環性を避けるように例を変更することなく、これらの解決順序に依存するエラーを報告しなくなれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。