facebook / facebook/flow

Proposal: Allow only Error instances to be thrown

Aperta
#4,476 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
feature request linter
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.