microsoft / microsoft/TypeScript

Indexed this types don't narrow when their referenced property narrows

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

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

In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 3.1.0-dev.20180829

Search Terms: this index narrowing generics

Code

type Keys = 'string' | 'number';
type Switch<T extends Keys> =
	T extends 'string' ? string :
	T extends 'number' ? number :
	never;

interface Main<T extends Keys> {
	kind: T;
	value: Switch<this['kind']>;
}

let x: Main<Keys> = null as any;

x.kind;
x.value;
if (x.kind === 'string') {
	x.kind; // Narrowed to "string"
	x.value;
} else {
	x.kind; // Narrowed to "number"
	x.value;
}

Expected behavior:
In the first if block, x.value should be of type string. In the second if block, x.value should be of type number.

Actual behavior:
In both cases x.value is not narrowed, despite x.kind narrowing successfully.

Related Issues: #24085

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

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

はじめの一歩

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

調査の方向性

Issue にある TypeScript の再現コードから始め、関連する issue #24085 とその動作を比較します。x.kind の絞り込みがインデックス付きの this['kind'] の型にどのような影響を与えるかを追跡し、その後、2 つの分岐がそれぞれ x.value について string と number を報告することを確認します。

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

評価

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

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

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