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

Comments

Projects
None yet
5 participants
@vkurchatkin
Collaborator

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

This comment has been minimized.

Show comment
Hide comment
@vkurchatkin

vkurchatkin Feb 26, 2016

Collaborator

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
Collaborator

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

This comment has been minimized.

Show comment
Hide comment
@zerkalica

zerkalica Apr 19, 2016

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

zerkalica commented Apr 19, 2016

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

@zerkalica

This comment has been minimized.

Show comment
Hide comment
@zerkalica

zerkalica commented Apr 19, 2016

duplicate: #1118

@avikchaudhuri

This comment has been minimized.

Show comment
Hide comment
@avikchaudhuri

avikchaudhuri Jun 1, 2016

Contributor

This will be fixed in an upcoming release.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@zerkalica

zerkalica 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
        }
    }
}

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

This comment has been minimized.

Show comment
Hide comment
@Mohamed-Abo-El-Soud

Mohamed-Abo-El-Soud Nov 19, 2016

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

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