microsoft / microsoft/TypeScript

Differing results for inference produced when identical overload count changes

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

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

Bug Domain: check: Type Inference
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

I happened to notice this while looking at something else inference related a bit ago. We already have a test that is looking for checking this, fixingTypeParametersRepeatedly3.ts, added in #2356, but it was accepted in #16368 (strict generic checks) with a (tiny, unnoticeable in all the other changes) baseline showing the fault.

Code

interface Base {
    baseProp;
}
interface Derived extends Base {
    toBase?(): Base;
}

var derived: Derived;

declare function foo<T>(x: T, func: (p: T) => T): T;
var result = foo(derived, d => d.toBase());

// bar should type check just like foo.
// result2 should have the same type as result
declare function bar<T>(x: T, func: (p: T) => T): T;
declare function bar<T>(x: T, func: (p: T) => T): T;
var result2 = bar(derived, d => d.toBase());

Expected behavior:
result and result2 are assigned the same type (there's a comment in the test asserting as much) - they are implemented identically except bar has an extra identical overload. (note: this can happen easily in the real world nowadays via intersection types! Intersecting multiple interfaces with similar base interfaces can cause exactly this situation.)

Actual behavior:
result typechecks as Derived
result2 typechecks as Base (seems wrong, since Base definitely doesn't have the toBase method used in the callback!)

Derived and Base are assignable to one another here, since toBase is optional, which is why the inference can succeed at all; however, Base, as reported in the second case can't actually have been the type used to typecheck the lambda body, as the toBase call doesn't cause an error in the test - I think the likely reason for the change in behavior is this removal, based on the content of the removed comment.

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

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

はじめの一歩

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

調査の方向性

既存の fixingTypeParametersRepeatedly3.ts テストと、result と result2 が同じ型であるというアサーションから始めてください。単一オーバーロード宣言と二重オーバーロード宣言で異なる推論を再現し、続いてリンクされた TypeScript PR で説明されている削除に関連する推論の挙動を追跡してください。完了条件は、callback のチェックを弱めることなく、両方の結果が一貫して型チェックを通ることです。

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

評価

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

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

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