microsoft / microsoft/TypeScript
Generic type comparison results in incorrect error when inferring from undefined type
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
"generics", "infer undefined type", "not assignable"
🕗 Version & Regression Information
- This changed between versions 3.6.3 and 3.7.5
⏯ Playground Link
💻 Code
type Generic<T> = {
// In this scenario, "InvalidType" is not defined
x: T extends InvalidType<infer S> ? S : 0
}
// Correct behaviour:
// Exactly matching generic type works correctly
const correct1: Generic<number> = {} as Generic<number>
// Non-matching generic type results in error (expected)
const correct2: Generic<number> = {} as Generic<string>
// Unexpected behavior
// More specific generic type results in a weird error:
// Type 'Generic<42>' is not assignable to type 'Generic<number>'.
// Type 'number' is not assignable to type '42'.
const invalid: Generic<number> = {} as Generic<42>
🙁 Actual behavior
TS error:
Type 'Generic<42>' is not assignable to type 'Generic<number>'.
Type 'number' is not assignable to type '42'.
🙂 Expected behavior
There should be no TypeScript error, or at least not a confusing error message like that.
It is not clear where the actual error comes from (undefined type InvalidType), and the current
error message does not really make sense - to a user it looks like the type comparison (42 -> number) was inverted (number -> 42).
Additional information about the issue
This also seems to happen in more complex scenarios where all involved types are correctly defined but another TS error like Type instantiation is excessively deep and possibly infinite occurs somewhere in the chain.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている TypeScript Playground の再現例から始め、言及されているバージョン 3.6.3 と 3.7.5 で報告された挙動を比較します。InvalidType が未定義の場合の Generic の型チェック経路を、追加の深いインスタンス化のシナリオも含めて追跡します。完了条件は、例でエラーが発生しないか、明確で正しく方向付けられた診断が出力されることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100