-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Maybe Type Refining fail in Array method(map/filter/...etc.) #5541
Comments
Variable with Maybe type can be changed. You can use |
Function parameters can be overwritten just like in your first example. There is a .flowconfig option you can enable to have flow treat function parameters as constants which would also fix your problem. |
@jcready Thanks for the info. Could you go into more detail for me and others following along why Flow doesn't maintain the refinement into the body of the function when there's no new assignment of function test(a: ?Object) {
if (!a) {
return null;
}
const keys = Object.keys(a);
for (const k of keys) a[k]; // Works?
// keys.filter(k => a[k]); // Flow errors?
} |
Here's one more example that's a little confusing to myself. function test(a: ?Object) {
if (!a) {
return null;
}
let b = a; // Magically, this fixes the issue but couldn't `b` just as well be overwritten?
return Object.keys(b).filter(k => b[k]);
} |
So I'm taking a guess here, but I believe the reason your last example works is because there can not be refinement invalidation of |
For example, this will throw the same error: function test(a: ?Object) {
let b: typeof a;
if (!a) {
return null;
}
b = a;
return Object.keys(b).filter(k => b[k]);
} |
in the latest flow this has no errors |
no longer errors |
code see also here in flow.org/try
It currently report error as follow:
The text was updated successfully, but these errors were encountered: