facebook / facebook/flow

Feature request: User-defined type assertions

Open
#6,052 2 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

In our Flow projects we're employing a fail-fast approach during development but fail-safe approach in production by having asserts that throw with a debug flag but simply log when that flag is off:

```js
function debugAssert(cond: boolean, msg: string) {
if (!cond) {
if(global.debug_assertions) throw new Error(msg);
else global.log.error(msg);
}
}
```

We'd like to use this assert in type refinement, e.g. `debugAssert(typeof someVal === 'string', 'value was not string')` but Flow doesn't recognize it as a predicate since it doesn't return a boolean; even if it did, Flow doesn't like it because it's nontrivial. However, it does divert control flow when we want it to, so it would serve the same purpose.

I should note that even if we always threw an error on assertion failure, Flow still wouldn't recognize it as a predicate function because it doesn't return a boolean.

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.