flow 0.67.1 (although behavior still present in version 0.73.1)
For Instance:
type PropOptional = {
prop?: ComplexType
};
type ComplexType = {
callable: () => void,
anotherCallable: () => void
};
function usePropOptional(arg1: PropOptional) {
if (arg1.prop) {
arg1.prop.callable();
arg1.prop.anotherCallable();
arg1.prop.callable();
}
};
The function ensures the existence of arg1.prop
before accessing any properties on it. This should confirm that arg1.prop
is defined.
Flow acknowledges the first time a property of arg1.prop
is accessed, specifically with the call to arg1.prop.callable()
on the initial line within the if
block. However, Flow raises errors when trying to access arg1.prop
properties again within the same if
block:
arg1.prop.anotherCallable();
arg1.prop.callable();
I must either include a repetition of arg1.prop &&
truthy checks before each line, or assign arg1.prop
to a local variable within the if block:
function usePropOptional(arg1: PropOptional) {
if (arg1.prop) {
const reallyExists = arg1.prop;
reallyExists.callable();
reallyExists.anotherCallable();
reallyExists.callable();
}
};
This solution feels inadequate. Am I missing something or doing something wrong?
You can test this out in a flow repl available on flow.org.