Bug Report
π Search Terms
optional chaining discriminated union control flow
π Version & Regression Information
- This is the behavior in every version I tried.
β― Playground Link
Playground link with relevant code
π» Code
interface Position {
x: number;
y: number;
computed?: never;
}
interface RelativePosition {
x: number | string;
y: number | string;
computed: Position;
}
declare var pos: Position | RelativePosition;
function getComputedX(pos: Position | RelativePosition): number {
return pos.computed?.x ?? pos.x;
// error here. Expect to be type "number", but actually "number | string";
}
if (pos.computed?.x !== undefined) {
pos; // Expected to be type "RelativePosition", but actually "Position | RelativePosition"
pos.computed;
// Inconsistence here.
// If "pos" is "Position | RelativePosition", then "pos.computed" should be "Position | undefined".
// If "pos" is "RelativePosition", then "pos.computed" should be "Position". (And this is expected.)
// But here "pos" is "Position | RelativePosition", but "pos.computed" is "Position".
// The inconsistence make me think it is a bug instead of a suggestion.
}
Possibly related: #34597
Bug Report
π Search Terms
optional chaining discriminated union control flow
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
Possibly related: #34597