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 type is not compatible to second union type, even with full overlap. #30170

Closed
noblica opened this issue Mar 1, 2019 · 2 comments · Fixed by #30779
Closed

Union type is not compatible to second union type, even with full overlap. #30170

noblica opened this issue Mar 1, 2019 · 2 comments · Fixed by #30779
Labels
Duplicate An existing issue was already created

Comments

@noblica
Copy link

noblica commented Mar 1, 2019

TypeScript Version: 3.3.3

Search Terms:
Union types overlapping

Code

interface Blue {
    color: 'blue'
}
interface Yellow {
    color?: 'yellow',
}
function draw(val: Blue | Yellow) { }

function drawWithColor(currentColor: 'blue' | 'yellow' | undefined) {
    // The following line throws the type error: Type '"blue"' is not assignable to type '"yellow"'.
    return draw({ color: currentColor });
}

Expected behavior:
No type errors exist, because there is an interface which would accept 'blue'.

Actual behavior:
An error is thrown:

Argument of type '{ color: "blue" | "yellow"; }' is not assignable to parameter of type 'Blue | Yellow'.
  Type '{ color: "blue" | "yellow"; }' is not assignable to type 'Yellow'.
    Types of property 'color' are incompatible.
      Type '"blue" | "yellow"' is not assignable to type '"yellow"'.
        Type '"blue"' is not assignable to type '"yellow"'.

Playground Link:
Link to the playground example

@jack-williams
Copy link
Collaborator

It might be the case that a literal with type { x : A | B } is assignable to { x: A } | { x: B }, but this does not hold when you have more properties. TypeScript wont special-case for single property objects.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 1, 2019
@RyanCavanaugh
Copy link
Member

#8289 / #12052 / #18421

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants