microsoft / microsoft/TypeScript

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

Abierto
#63,004 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Control Flow
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce el diagnóstico en el TypeScript Playground enlazado y compara el comportamiento entre las versiones descritas, especialmente 3.6 y 3.7. Lee el issue relacionado #61259 y sigue el tratamiento del flujo de control de los bloques catch y finally; se considera terminado cuando el ejemplo compila sin TS7027 y conserva el tipo de retorno never explícito.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.