flow
flow copied to clipboard
Feature request: User-defined type assertions
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:
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.