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

@vkurchatkin vkurchatkin commented Feb 26, 2016

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

@vkurchatkin vkurchatkin commented Feb 26, 2016

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

@zerkalica zerkalica commented Apr 19, 2016

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

@zerkalica
Copy link

@zerkalica zerkalica commented Apr 19, 2016

duplicate: #1118

@avikchaudhuri
Copy link
Contributor

@avikchaudhuri avikchaudhuri commented Jun 1, 2016

This will be fixed in an upcoming release.

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

@zerkalica zerkalica commented Jun 14, 2016

@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
        }
    }
}
@Mohamed-Abo-El-Soud
Copy link

@Mohamed-Abo-El-Soud Mohamed-Abo-El-Soud commented Nov 19, 2016

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
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.