microsoft / microsoft/TypeScript

Type not narrowed by === when equivalent type guard works

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

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

Bug Domain: check: Control Flow Rescheduled
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 3.7.5, also tested on 3.9.0-dev.20200212

Search Terms: equals, type guard, type parameter, narrowing

Code

type AorB = 'a' | 'b';

function isA(t: AorB): t is 'a' {
  return t === 'a';
}

function test<T extends AorB>(arg: T) {
  if (isA(arg)) {
    let a1: T & 'a' = arg; // ok
  }
  if (arg === 'a') {
    let a2: T & 'a' = arg; // error
  }
}

Expected behavior: The condition arg === 'a' should narrow the type of arg to T & 'a' in the second if block, in the same way as the equivalent type guard function does.

Actual behavior: The type of arg is not narrowed in the second if block.

Playground Link: Playground Link

Related Issues: N/A

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

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

はじめの一歩

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

調査の方向性

まず、issue の TypeScript スニペットから TypeScript 3.7.5 または 3.9.0-dev.20200212 の動作を再現し、直接の等価性チェックと同等の type guard を比較します。ジェネリック型パラメーターに対するコンパイラーの型の絞り込み処理を追跡します。完了の条件は、arg === 'a' によってエラーなしで argT  'a' に代入できることです。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
38/100

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

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