microsoft / microsoft/TypeScript

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

Offen
#63,004 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Control Flow
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### 🔎 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere die Diagnose im verlinkten TypeScript Playground und vergleiche das Verhalten zwischen den beschriebenen Versionen, insbesondere 3.6 und 3.7. Lies den zugehörigen Issue #61259 und verfolge die Control-Flow-Behandlung für die catch- und finally-Blöcke; abgeschlossen ist die Aufgabe, wenn das Beispiel ohne TS7027 kompiliert wird und dabei den expliziten never-Rückgabetyp beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.