microsoft / microsoft/TypeScript

Reflect.has fails to act as type guard (should act same as "in" operator)

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

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

Domain: lib.d.ts Experience Enhancement Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

TypeScript Version: ^3.4.0-dev.20190330

Search Terms:
Reflect.has
in operator

Code

const test1 = (a: { field: number } | {}) => (("field" in a) ? a.field : 0);

const test2 = (a: { field: number } | {}) => Reflect.has(a, "field") ? a.field : 0;

Expected behavior:
Both compile successfully. "in" operator acts as a type guard per https://github.com/Microsoft/TypeScript/issues/10485. Reflect.has should act the same as "in" operator here.

We'd like to use Reflect.has a lot more and this case holds us back. Thank you!

Actual behavior:
"in" operator line compiles; Reflect.has line does not compile. Error:

allPasos.ts:445:31 - error TS2339: Property 'field' does not exist on type '{} | { field: number; }'.
Property 'field' does not exist on type '{}'.

445 Reflect.has(a, "field") ? a.field : 0;

Playground Link:

https://goo.gl/2R4dkS

Related Issues:

https://github.com/Microsoft/TypeScript/issues/10485

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

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

はじめの一歩

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

調査の方向性

リンク先の Playground の再現から始め、既存の in 演算子の narrowing の挙動を Reflect.has と比較してください。背景については関連する issue #10485 を読んでください。示されている Reflect.has 式がコンパイルされ、issue が期待する通りに動作し、回帰に対するカバレッジがあることが完了条件です。

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

評価

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

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

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