Skip to content
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

instanceof Array refinement adds Array<any> to type #6874

Closed
postcasio opened this issue Sep 11, 2018 · 4 comments
Closed

instanceof Array refinement adds Array<any> to type #6874

postcasio opened this issue Sep 11, 2018 · 4 comments

Comments

@postcasio
Copy link

postcasio commented Sep 11, 2018

https://flow.org/try/#0PTACDMBsHsHcCh4BMCmBjSBDATigBBpgM5F4DC0M2eA3vHnkQC6ZMCWaeA1ACraYA7IgAccKAUwBc5StGwBueAF9ETAJ7D8ABWzRhpALy16eAEZzU2ClQD8063LwAfPAEFs-NQB4H2AHwANCbm2JYA6mxITAAWdngCAK4AtqYo1C7unl6JKWl+yorwaNBCTGYWaUQ8KMxkxPhGABTCuvrSOnpEAJR4Bn7GDJAoZSGWvtKZmN6+-UYA2gC6hQxs4HjNrUQAdKNpvj10DCtrG507FVay1Gylgmgo0GuTagcmR+Whe1e9eC1nu5cqIp3ko8ChIER8Id3h8xt8jAIULA3B4po0ACxdLbgNiQSCnfTnT6AuRdYFHFQMUHgyEDI4A3w-RHI54YrE4vGNXxbPiCERiCRkkwqExDEYXCJRaITVHeHKpfw-RbLPCrdZ-QkAyUxV7vNUE7ZayIxVW3AT3R4ozy6mFGqU-DWGiXG6LkqlgiFQt70532hFIq1ozHY3H4x1E8IuoUg4Ue2nQhh2k3+lmytkhzkABmjeBUSkUQA

This case demonstrates the same code structure working with two different types: void | number | Array<number> and void | Color | Array<Color>.

Flow chokes on the code dealing with the Color class. Inspecting what flow thinks the types are using the vscode plugin, it shows that on line 15, the type is correct: void | Color | Array<Color>. On line 16, it is correct: Color | Array<Color>. On line 17, after the instanceof Array check, it suddenly becomes Array<Color> | Array<any>, which is incompatible with the Array<Color> variable I'm trying to assign it to.

This doesn't happen for the number case - after the instanceof Array check, it correctly infers that the type is only Array<number>.

Where is this Array<any> coming from?

If I use Array.isArray instead of instanceof Array, it type checks correctly.

@jbrown215
Copy link
Contributor

This looks related to #6741, likely the same bug.

@mgreenw
Copy link

mgreenw commented Feb 20, 2019

I think this is related to an issue I'm having with $ObjMap. Using the Official Documentation's $ObjMap Example I am running $ObjMap as such:

type TypeAssertions = { [key: string]: Function };
function run<O: {[key: string]: Function}>(o: O): $ObjMap<O, ExtractReturnType> {
  return Object.keys(o).reduce((acc, k) => Object.assign(acc, { [k]: o[k]() }), {});
}

const o = {
  a: () => true,
  b: () => 'foo',
  c: () => ['hey there'],
};

By itself, c is of type () => Array<string>.
However, when the following code is added, c's type changes to () => Array<any|string>:

const p = run(o).c;

Just running the method causes flow to add the any type to the return type. I'm not sure if this is a bug with $ObjMap or any of the mentioned bugs above.

Tested with Flow 0.93.

@jbrown215
Copy link
Contributor

@mgreenw: That's not related. Feel free to open a separate issue.

@postcasio
Copy link
Author

Closing because this appears to have been fixed as of v0.97.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants