facebook / facebook/flow

Proposal: Allow only Error instances to be thrown

Ouverte
#4,476 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
feature request linter
Langage dominant
Rust
Étoiles
22.3k
Forks
1.9k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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())
}
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.