flow
flow copied to clipboard
Null check throws error when checked in an outer function
With this:
/* @flow */
type ItemMap = {
[id: number]: string;
}
type ItemAction = {
type: string;
items?: ItemMap;
}
function getFirstActionItem (action: ItemAction) {
if(action.items == null) {
return;
}
const fun = (itemId: number) => {
return action.items[itemId];
};
return fun(0);
}
I get errors:
src/index.js:17
17: return action.items[itemId];
^^^^^^^^^^^^^^^^^^^^ access of computed property/element. Computed property/element cannot be accessed on possibly undefined value
17: return action.items[itemId];
^^^^^^^^^^^^ undefined
Found 1 error
Expect no errors since I did the null check on line 12.
This is expected behavior. See the documentation here: http://flowtype.org/docs/dynamic-type-tests.html#caveats
That is a "caveat" :) Sorry for the noise.