Closed
Description
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