microsoft / microsoft/TypeScript
narrowing to "never" by "if" statement does not exclude "undefined" return type
オープン
まだ誰も着手していません。
In Discussion
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
TypeScript Version: 2.7.1
Search Terms: narrowing types never switch case if undefined strictNullChecks
Code
// Build this code with "--strictNullChecks" option.
function assertNever(x: never): never {
throw new Error("not reached");
}
enum A {
Foo, Bar
}
// There are no errors on this function.
function good(a: A): number {
switch (a) {
case A.Foo: return 0;
case A.Bar: return 1;
}
}
// This function causes the following error though "a" is "never" at the end of the function.
// error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
function bad(a: A): number {
if (a === A.Foo) return 0;
if (a === A.Bar) return 1;
}
// No error if explicit return of "never" is added.
function ok(a: A): number {
if (a === A.Foo) return 0;
if (a === A.Bar) return 1;
return assertNever(a);
}
Expected behavior: The bad() function should not cause error so that if statement works like switch.
Actual behavior: As commented in the code, only bad() function causes an error.
Playground Link: (please check "strictNullChecks" option.)
Related Issues:
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、strictNullChecksを有効にしてissueのTypeScriptコードを再現し、ifとswitchのケースを比較します。問題のある関数について、コンパイラーのcontrol-flow narrowingとmissing-return analysisを追跡します。このケースで報告されたdiagnosticが解決され、regression coverageによって動作が確認できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100