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

Intersection breaks disjoint unions #1462

Closed
vkurchatkin opened this issue Feb 26, 2016 · 6 comments
Closed

Intersection breaks disjoint unions #1462

vkurchatkin opened this issue Feb 26, 2016 · 6 comments

Comments

@vkurchatkin
Copy link
Contributor

This type-checks, although it shouldn't:

type Common = {
};

type A = Common & {
  type: 'A',
  foo: number
};

type B = Common & {
  type: 'B',
  foo: Array<number>
}

type MyType = A | B;

function print(x: number) {
  console.log(x);
}

function printAll(val: MyType) {
  print(val.foo);  // <--- foo could be an array
}

If I remove intersection it works as expected.

@vkurchatkin
Copy link
Contributor Author

Interesting thing: order matters. If you declare types like this:

type A = {
  type: 'A',
  foo: number
} & Common;

type B = {
  type: 'B',
  foo: Array<number>
} & Common;

This example begins to fail correctly, but refinement doesn't work anymore:

type Common = {
};

type A = {
  type: 'A',
  foo: number
} & Common;

type B = {
  type: 'B',
  foo: Array<number>
} & Common;

type MyType = A | B;


function print(x: number) {
  console.log(x);
}

function printAll(val: MyType) {
  if (val.type === 'A') {
    print(val.foo);
  } else {
    val.foo.forEach(print);
  }
}

Outputs:

test/good/tmp.js:25
 25:     print(val.foo);
         ^^^^^^^^^^^^^^ function call
 25:     print(val.foo);
               ^^^^^^^ array type. This type is incompatible with
 19: function print(x: number) {
                       ^^^^^^ number

test/good/tmp.js:27
 27:     val.foo.forEach(print);
                 ^^^^^^^ property `forEach`. Property not found in
 27:     val.foo.forEach(print);
         ^^^^^^^ Number


Found 2 errors

@zerkalica
Copy link

Any progress on this? How to create unions with common part?

@zerkalica
Copy link

duplicate: #1118

@avikchaudhuri
Copy link
Contributor

This will be fixed in an upcoming release.

@avikchaudhuri avikchaudhuri self-assigned this Jun 1, 2016
@ghost ghost closed this as completed in 2df7671 Jun 10, 2016
@zerkalica
Copy link

@avikchaudhuri flow 0.27, not fixed for me:
May be related: #1931, #1664

// @flow
type DataBase = {
  id: string;
  name: string;
};

type UserData = DataBase & {
  kind: 'user';
};

type SystemData = DataBase & {
  kind: 'system';
  n: number;
}

type Data = UserData | SystemData;
function test(data: Data): void {
    if (data.kind === 'system') {
        if ((data: SystemData).n === 1) { // Error: this type is incompatible with
            return
        }
    }
}

@maboelsoud
Copy link

Any updates on this, I'm using the latest version and this is still a problem.

This issue was closed.
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

5 participants