facebook / facebook/flow

Request: option for refinement by console.assert

Open
#6,729 0 comments 0 reactions 0 assignees View on GitHub
feature request
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Hello,

would it be feasable to add an option giving `console.assert` the same semantics as `throw`-statements with regard to [refinements](https://flow.org/en/docs/lang/refinements/#refinement-invalidations-)? The below example currently points out that `.parentNode` may be `undefined`:
```js
function remove(node: Node): Node {
return node.parentNode.removeChild(node)
}
```
Inserting an if-throw/-return statement refines the type of `.parentNode` for the else-branch. The type-checker is happy with this:
```js
function remove(node: Node): Node {
if (node.parentNode == null) throw new Error('cannot remove orphaned node')
return node.parentNode.removeChild(node)
}
```
Although `console.assert` does not change execution flow, the intend is otherwise the same. In addition, console-statements can be stripped once the tests pass.
```js
function remove(node: Node): Node {
console.assert(node.parentNode, 'cannot remove orphaned node')
return node.parentNode.removeChild(node)
}
```
This of course assumes, the application is not supposed to recover from such an error.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.