microsoft / microsoft/TypeScript

Explicit `never` return type in `catch` block & condition in `finally` marks the whole `finally` block as unreachable

オープン
#63,004 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る
Bug Domain: check: Control Flow
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔎 Search Terms

finally, unreachable, condition, never

### 🕗 Version & Regression Information

- This builds fine in 3.2,
- produces a type error regarding the return type in 3.3 through 3.6,
- and starting 3.7, including the latest native preview, it produces the unreachable code error

### ⏯ Playground Link

https://www.typescriptlang.org/play/?noImplicitAny=false&ts=5.9.3#code/MYewdgzgLgBATgUwA4jlAXDAFAzBDMATwEoYBeAPhjAQDcE5zsFjMb7HKYBvAKBhhQAFnBAB3GAgDcvAL4zeoSLABmIEEzwRCYYNlYwACqIC2ASwgIAPGACuJgEYMqXPgKhxCPfgPgIotnBgMADMMgKyMMB4UMBCzKRuvogoaDjE4TCRKmZgeAA2+V5JAmYq2B62LN6+vkoQIPkIAHT5IADmWABEQghCXRk+ET6yclJAA

### 💻 Code

```ts
function report(e): never {
throw e;
};

const foo = async (): Promise => {
try {
return 3;
} catch (e) {
report(e);
} finally {
if (true) {
console.log("heh");
}
}
};
```

### 🙁 Actual behavior

```
test.ts:11:5 - error TS7027: Unreachable code detected.

11 if (true) {
~~~~~~~~~~~
12 console.log("heh");
~~~~~~~~~~~~~~~~~~~~~~~~~
13 }
~~~~~

test.ts:12:7 - error TS7027: Unreachable code detected.

12 console.log("heh");
~~~~~~~~~~~~~~~~~~~
```

### 🙂 Expected behavior

Builds fine.

### Additional information about the issue

This error vanishes if I do any of the following:

- Remove the explicit `never` return type of `reportError` (although that resurfaces the return type error)
- Remove the call to `reportError` entirely, or the whole `catch` block
- Remove the `if` condition from the `finally` block, but leave in the `console.log` call

I found a similar issue #61259, but it does not include a `catch` block, which is integral to the issue here - although the issues might still be related. Similarly to that issue, the `finally` block also becomes reachable when adding another `return` to the `catch` block, but it is itself unreachable.

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

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

調査の方向性

リンク先の TypeScript Playground で診断を再現し、記載されているバージョン間、特に 3.6 と 3.7 での動作を比較してください。関連する issue #61259 を読み、catch ブロックと finally ブロックの制御フロー処理を追跡してください。明示的な never 戻り値型を維持したまま、例が TS7027 なしでビルドできれば完了です。

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

評価

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

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

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