flow icon indicating copy to clipboard operation
flow copied to clipboard

Union of unions doesn't work as I expected

Open jergason opened this issue 11 years ago • 10 comments

I am trying to create a union of union types, and it doesn't work as I expect it to.

/* @flow */

type Foo = 'foo' | 'stuff'
type Bar = 'bar'

type Status = Foo | Bar

type Thingy = {
  id: string;
  status: Status
}

function doStuff(thing: Thingy): Thingy {
  thing.status = 'bar'
  return thing
}

Running flow on this gives the following error:

/Users/jergason/code/flow-test/durp.js:14:18,22: string
This type is incompatible with
/Users/jergason/code/flow-test/durp.js:3:12,26: union type

I expected to be able to create a union out of two union types, but it appears the Status type doesn't pick up values from the Bar type. If I change the code to thing.status = 'foo' or thing.status = 'stuff' it works. If I change the types to:

type Foo = 'foo'
type Bar = 'bar' | 'baz'

then I can assign thing.status = 'bar' or thing.status = 'baz' without problem. It looks like it is only happening when the first type in the union is also a union.

This is on flow 0.12 on Mac OS X.

jergason avatar Jun 28 '15 04:06 jergason

Yeah, I can reproduce this too. Here's a slightly simpler test case:

/* @flow */

// no eror
var ok: ('foo' | 'bar') | 'baz' = 'baz'

type FooBar = 'foo' | 'bar'
type Baz = 'baz'
type FooBarBaz = FooBar | Baz

// error
var bad: FooBarBaz = 'baz'

/*
test.js|11 col 22 error|  string
|| This type is incompatible with
test.js|6 col 15 error|  union type
|| 
test.js|7 col 12 error|  string literal baz
|| This type is incompatible with
test.js|6 col 15 error|  union type
|| 
*/

samwgoldman avatar Jun 28 '15 21:06 samwgoldman

Just chiming in here to "vote" for this one to get fixed

leoasis avatar Jul 29 '15 10:07 leoasis

Ran into the same thing trying to use a union to represent the different action types in Flux:

type FooActionTypes = {
  type: 'foo/1';
} | {
  type: 'foo/2';
};

type BarActionTypes = {
  type: 'bar/1';
} | {
  type: 'bar/2';
} | {
  type: 'bar/3';
};

type Action =  FooActionTypes | BarActionTypes;

function handle(action: Action) {}

// this call errors with something like "Object literal type incompatible with FooActionTypes union type"
handle({
  type: 'bar/1'
});

handle({
  type: 'foo/1'
});

ianobermiller avatar Jul 29 '15 16:07 ianobermiller

It'd be nice if the docs advised against nesting unions and intersections until this is fixed. It's an easy trap to fall into and it costs a few hours.

niran avatar Oct 27 '15 03:10 niran

Is there an ongoing work to fix this that I can follow ?

Gozala avatar Nov 05 '15 18:11 Gozala

I just ran into this issue and it appeared to only surface once I split types across files.

paulyoung avatar Nov 07 '15 23:11 paulyoung

This issue renders flow extremely clunky and essentially unusable for our type system, and we're thinking about switching to TypeScript for this reason alone...

zgao avatar Nov 15 '15 19:11 zgao

Yeah we also started considering typescript as an alternative as we keep hitting this issue over and over, although migration seems like a lot of work so for now we've decided to not type check and hopefully this will be resolved soonish.

@samwgoldman BTW your pull #583 does seem to resolve the issue (at least all occasions we've run into). You did say your fix was pretty hacky on IRC, but do you thing it could still land as temporary fix ? I'm sure users of flow would much rather have a hacky fix for this issue than it being just broken.

Gozala avatar Nov 16 '15 21:11 Gozala

I should comment that we have also started maintaining a separate build of flow with #583, and it works for everything we care about.

zgao avatar Nov 16 '15 22:11 zgao

@Gozala et al FYI there are a couple of issues in play here - first has to do with nested unions, second with unions that include externally defined types. Fixes in the works for both. What prevented us from taking @samwgoldman 's nesting fix as-is was (as he mentioned) the regression in error message quality, but we're on it. Thanks for your patience!

bhosmer avatar Dec 07 '15 19:12 bhosmer