flow icon indicating copy to clipboard operation
flow copied to clipboard

Union passing depends on the order of options

Open adrianheine opened this issue 10 years ago • 5 comments

I noticed you have some known issues with unions and are working on them. Here's another quite reduced case that fails:

::::::::::::::
1.js
::::::::::::::
/* @flow */
import type {Foobar} from "./2"

function create(content: ?Foobar | String | Array<String>) {
}

function node(content: ?Foobar | String | Array<String>) {
  create(content)
}
::::::::::::::
2.js
::::::::::::::
/* @flow */

export class Foobar { }
$ flow init
$ flow check
1.js:8
  8:   create(content)
              ^^^^^^^ array type. This type is incompatible with
  4: function create(content: ?Foobar | String | Array<String>) {
                               ^^^^^^ Foobar


Found 1 error

If I move the array to the beginning of the union, it passes. Likewise, if I merge the two files, it passes.

adrianheine avatar Feb 25 '16 19:02 adrianheine

so, the way Flow handles unions is to try each branch and choose whichever one succeeds as the resulting type; if none succeed, then it raises an error. the problem is that it currently defines "succeeds" as "doesn't fail." Due to the way Flow infers each file individually and then merges together the results across files, Foobar is still unresolved when it checks each branch, so it "doesn't fail" and thinks content is a Foobar... obviously incorrect. this is why it passes if you merge the two files, since Foobar is known so it doesn't match and goes onto the next branch of the union.

this is something we've been trying to fix for a while and are currently prioritizing (cc @bhosmer).

mroch avatar Feb 25 '16 20:02 mroch

Just chipping in and confirming that this is a major issue for us as well. We recently refactored our code and replaced a bunch of union fields with classes, and Flow essentially has stopped working for us in the meantime.

zgao avatar Mar 08 '16 23:03 zgao

@samwgoldman has there been any progress at all in fixing this? Thanks!

zgao avatar Mar 26 '16 00:03 zgao

I'm actively working on fixing this problem, expect some news in a few weeks.

avikchaudhuri avatar Mar 26 '16 04:03 avikchaudhuri

This will be fixed in an upcoming release.

avikchaudhuri avatar Jun 01 '16 09:06 avikchaudhuri