Optional property cannot be accessed across function boundary
Example: http://tryflow.org/#cf00de39d057c948f1ad8f9da9de4e93
/* @flow */
type getOptions = {retries?: number};
exports.get = function(opts?: getOptions) {
if (opts === undefined) {
opts = {};
}
if (opts.retries === undefined) {
opts.retries = 0;
}
console.log(opts.retries);
var f = function() {
console.log(opts.retries); // <---
};
}
At the arrow, "Property cannot be accessed on optional getOptions", yet there is an undefined check.
This could be intentional - If after function f opts.retries was set to undefined, then f invoked, the warning would be correct. Wrapping f in an IIFE which passes in opts appears to work, but that will get messy very quickly. Is there a better way?
We might be able to special case some simple scenarios (like the one you've outlined), but I believe the reason this doesn't work is because it's not quite safe in the general sense.
In a general sense: If that f function were ever to get out of that function, or be closed over by anything that escapes the outer function, it's no longer necessarily safe to assume that opts.retries is actually there. The best we could do (in theory -- I haven't confirmed viability of this) would be to narrow the type of opts only in this space if we could prove that it's safe to do so.
This is worth looking into, but there may not be a trivial solution (or an all-encompassing one).
(cc @bhosmer )
Hi @cyounkins - the big general issue with refining the types of object properties is aliasing - people outside the function might be holding a reference to opts and so opts.retries might be reset to undefined whenever the flow of execution goes out of the local code, e.g. in a function call.
(We do track refinements of object props up to such escapes, but the reason for this error is that we currently consider all closures to be escapes - here we're blind to the fact that the closure itself never leaves the function.)
This is probably obvious, but when it's applicable to the problem, the best way to deal with this currently is to bring the property value local. The following typechecks, for example:
type getOptions = {retries?: number};
exports.get = function(opts?: getOptions) {
if (opts === undefined) {
opts = {};
}
var retries = opts.retries || 0; // retries: number inferred here
console.log(retries);
var f = function() {
console.log(retries);
};
}
Thank you @bhosmer . Actually, your solution had not occurred to me - it's a duplication of data that I try to stay away from, but since I want to lock the value anyway, it's a reasonable solution.
One thing that could solve this issue and enable more checking is immutable JS - #18
So I see two issues here
- The given error message, "Property cannot be accessed on optional getOptions", does not accurately describe the problem.
- The documentation can be improved to show methods to deal with this problem.
I can do a PR for 2, but not 1. Would it be OK to add a section to the "Troubleshooting" doc about this?
Using a default parameter also works (you don't need to mark the parameter as optional either):
type getOptions = {retries?: number};
exports.get = function(opts: getOptions = {}) {
if (opts.retries === undefined) {
opts.retries = 0;
}
console.log(opts.retries);
var f = function() {
console.log(opts.retries);
};
}
http://tryflow.org/#670628b491d26f8d8e17ee86a0001750
@tlrobinson that link lead me to something quite NSFW, FYI. Anyone who is reading this be wary of clicking those tryflow(dot)org links
I believe the issue is not relevant since default params is widely used part of standard. https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDmeGA8jhgJZwB2AzmALxgDeATiW5XnQPwBcYGgFcAtgCM8bAL4BuVHgAeOOGwx0AdMQxMwUYTQDGVWgAo4FPoO3kT9XS2kBKVqjDuwlKGHOWNHDC4eJkZmAwATPChKGjxwlxY3D2SLdX9ObgZmAAZ5ZOl0ZMNaOjgYPA14Ql80gKC6J3kk9wA3AEM2PV19IztTBObksGL6Moqqms06zMawYGAwAB4AWlXBuVRpIA