microsoft / microsoft/TypeScript
TypeScript doesn't narrow out `undefined` after constructor call when callee is `any`
まだ誰も着手していません。
- 主要言語
- 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'.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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