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

Union doesn't merge objects properties' type #8289

Closed
nicolo-ribaudo opened this issue Apr 25, 2016 · 3 comments
Closed

Union doesn't merge objects properties' type #8289

nicolo-ribaudo opened this issue Apr 25, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@nicolo-ribaudo
Copy link

TypeScript Version:

This is reproducible both in VSCode 1.0 and in the playground (https://www.typescriptlang.org/play/#src=...)

Code

interface A {
    x: number;
}

interface B {
    x: string;
}

const c: A | B = { x: 1 || "a" };

const d: A | B = { x: 1 } || { x: "a" };

Expected behavior:
Both c and d should be fine

Actual behavior:
TypeScript reports this error for the const c: A | B = { x: 1 || "a" }; statement:

Type '{ x: number | string; }' is not assignable to type 'A | B'.
  Type '{ x: number | string; }' is not assignable to type 'B'.
    Types of property 'x' are incompatible.
      Type 'number | string' is not assignable to type 'string'.
        Type 'number' is not assignable to type 'string'.
@nicolo-ribaudo nicolo-ribaudo changed the title Union doesn't merge object properties' type Union doesn't merge objects properties' type Apr 25, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Apr 25, 2016

{ x: number | string }; is neither A nor B. it becomes easier to see that with more than one property, e.g.

interface A {
    x: number;
    y:  number;
}

interface B {
    x: string;
    y: string;
}

const c: A | B = { x: 1 || "a" , y :  2 || "b"};

@RyanCavanaugh
Copy link
Member

I should add that we've gotten this request around single-property types a bunch. It turns out to be very difficult to detect all of these cases properly in the compiler.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Apr 25, 2016
@nicolo-ribaudo
Copy link
Author

@mhegazy Thank you, now I see why the current behavior is correct

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants