Feature request: User-defined type assertions
- 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
Assessment
This issue has not been assessed yet.