microsoft / microsoft/TypeScript
Explicit `never` return type in `catch` block & condition in `finally` marks the whole `finally` block as unreachable
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
### 🔎 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.
Hướng dẫn đóng góp
Hướng nghiên cứu
Tái hiện chẩn đoán trong TypeScript Playground được liên kết và so sánh hành vi giữa các phiên bản được mô tả, đặc biệt là 3.6 và 3.7. Đọc issue liên quan #61259 và theo dõi cách xử lý control-flow cho các khối catch và finally; được xem là hoàn thành khi ví dụ build được mà không có TS7027, đồng thời vẫn giữ kiểu trả về never tường minh.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 48/100