microsoft / microsoft/TypeScript

TypeScript doesn't narrow out `undefined` after constructor call when callee is `any`

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

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

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

説明

TypeScript doesn't seem to narrow the type of a variable correctly in cases where the constructor being called is typed as any. If the type of a variable includes undefined in its type signature, it's not narrowed out after a constructor call even though by definition (I think--correct me if I'm wrong) new fn() must always evaluate to an object if it doesn't throw first.

TypeScript Version: 3.1.3

Search Terms: constructor narrow

Code

let x: { [x: string]: any } | undefined;
x = new (Object as any)();
// AFAIK `x` can't possibly be undefined after this point
console.log(x.foo);  // error: Object is possibly 'undefined'.

Expected behavior:
No error.

Actual behavior:
Compile-time error: Object is possibly 'undefined'.

Playground Link:
http://www.typescriptlang.org/play/#src=let%20x%3A%20%7B%20%5Bx%3A%20string%5D%3A%20any%20%7D%20%7C%20undefined%3B%0D%0Ax%20%3D%20new%20(Object%20as%20any)()%3B%0D%0Aconsole.log(x.foo)%3B%0D%0A

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

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

はじめの一歩

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

調査の方向性

TypeScript Playground の再現ケースと、issue に示されている new (Object as any)() の代入から始めます。callee が any であるコンストラクター呼び出しを control-flow narrowing がどのように扱うかを追跡し、その後、関連しない narrowing の挙動を変更せずに、例で Object is possibly 'undefined' が報告されなくなっていることを確認します。

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

評価

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

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

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