microsoft / microsoft/TypeScript

TypeScript does not recognize unreachable code after a method returning never is called outside of its defining scope

オープン
#59,138 コメント 4 件 リアクション 7 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.89.1
  • OS Version: All

To Reproduce:

class SimpleThrower {
    public throwError(): never {
        throw new Error('');
    }

    public doSomething(): string {
        this.throwError(); // Directly call the never-returning function
        
        return 'Success'; // TypeScript recognizes that this line is unreachable
    }
}

() => {
    const example = new SimpleThrower();
    console.log(example.doSomething()); // This will throw an error
    example.throwError(); // Throws an error and stops execution
    console.log('foobar'); // TypeScript does not recognize that this line is unreachable
}

Playground Link

Expected behavior:

TypeScript should recognize that the line console.log('foobar'); is unreachable after example.throwError(); is called, similar to how it recognizes the unreachable code within the doSomething method.

Actual behavior:

TypeScript does not mark the line console.log('foobar'); as unreachable after the example.throwError(); call in the global scope or within a different function scope.

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

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

はじめの一歩

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

調査の方向性

提供された Playground Link から始め、外側の関数での example.throwError() 後の挙動と、this.throwError() 後の doSomething 内で到達不能なコードが認識される仕組みを比較してください。never を返すメソッドの呼び出しを担当する TypeScript の制御フロー解析のエントリポイントを追跡してください。後者の場合に console.log('foobar') が到達不能としてマークされ、既存のメソッドの挙動を後退させないことが完了条件です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
42/100

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

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