facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.09k stars 1.86k forks source link

Feature request: refining multiple disjoint unions #5948

Open danwang opened 6 years ago

danwang commented 6 years ago

Hello Flow team!

[Flow try link]

// @flow

type A = {
  tag: 'a',
};
type B = {
  tag: 'b',
};
type Foo = A | B;

const bar = (x: Foo, y: Foo) => {
  if (x.tag === 'a') {
    return 'x is a';
  } else if (y.tag === 'a') {
    return 'y ix a';
  } else if (x.tag === 'b' && y.tag === 'b') {
    return 'both are b';
  } else {
    (x: empty);
    (y: empty);
  }
}

All cases are covered by the first three branches in the if/else, so the empty assertions in the last branch should be correct. As an example, Flow correctly identifies this when we assert that x and y are both B (Flow try link).

TrySound commented 6 years ago

Update flow type link please

danwang commented 6 years ago

@TrySound how do you mean? The links look okay to me.

TrySound commented 6 years ago

@danwang Sorry, misread :)

stereobooster commented 6 years ago

This one works as you expect