facebook / facebook/flow

Proposal: Allow only Error instances to be thrown

Abierto
#4,476 0 comentarios 0 reacciones 0 asignados Ver en GitHub
feature request linter
Lenguaje dominante
Rust
Estrellas
22.3k
Forks
1.9k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Or maybe just enforce a minimal structure:

```js
type Throwable = Error | {
message: string,
stack: string,
constructor: {
name: string
}
}
```

For example:

```js
function foo () {
throw 'foo'
^ string. This type cannot be thrown.
}
```

Same rules should also apply to things passed to `Promise.reject()`.

This would allow flow to assert that the error caught in a try/catch block isn't `any` but is in fact `Throwable`. For example the following does not have any flow errors, but will have runtime errors:

```js
function foo () {
throw 1
}

function bar () {
throw new Error()
}

try {
foo()
} catch (e) {
console.log(e.constructor.name, e.message, e.stack)
console.log(e.toFixed())
}

try {
bar()
} catch (e) {
console.log(e.constructor.name, e.message, e.stack)
console.log(e.toFixed())
}
```

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.