I have exactly the same issue here, anybody help?
my flow version is 0.38.0.
Flow has issues with predicate functions, see #34. However, is is possible to annotate a function as helping for refinements, with keyword %checks, see this example.
However, based on #2918, it might not be a public feature, and perhaps the $Pred type should be used, but I don't know how to use it :/
Any updates on a workaround? I'm trying to use lodash's isNil and flow doesn't seem to recognize the check.
Ran into same problem with Flow not understanding Lodash's isNil check. I even added the flow type for Lodash. It has a generated signature of
isNil(value: any): boolean
Thanks
Wonder whether supporting some kind of attribute in the flow type file would be an option to indicate to Flow that the method does null checking. Perhaps using the linting comments to indicate kind of like the skip on sketchy null, but in the reverse direction.
I just prefer the readability of functions. Thank you.
This is getting me too. I don't think the attribute suggestion is optimal. I think Flow should just be able to check this directly, like in other static typed languages.
Adding to this: my guess is %checks is probably less performant, and not all functions that return booleans are doing refinements, so there's probably some performance gain from explicitly enabling the extra complexity.
Using %checks is the right approach here and answers the initial question.
In regards to isNil, flow-typed has since added an overloaded def so that it can handle type refinement so testing with the below code would work.
// @flow
import { isNil } from 'lodash';
function t(a?: boolean): boolean {
if (isNil(a)) {
return false;
}
return a;
}
Most helpful comment
Flow has issues with predicate functions, see #34. However, is is possible to annotate a function as helping for refinements, with keyword
%checks, see this example.However, based on #2918, it might not be a public feature, and perhaps the
$Predtype should be used, but I don't know how to use it :/