Closed
Description
Bug Report
Potentially related to #44382.
π Search Terms
- union types
- narrowing
- 4.3
π Version & Regression Information
- This changed between versions 4.2 and 4.3
β― Playground Link
Playground link with relevant code
π» Code
interface X {
a?: { aProp: string };
b?: { bProp: string };
}
type AorB = { a: object; b: undefined } | { a: undefined; b: object };
declare const q: X & AorB;
if (q.a !== undefined) {
q.a.aProp;
} else {
// q.b is incorrectly inferred as potentially undefined:
q.b.bProp;
}
π Actual behavior
q.b
is inferred as potentially undefined in the else
block. Given that q
conforms with AorB
, and q.a
is known to be undefined in the else block, this is incorrect.
π Expected behavior
q.b
should be inferred as present. In particular, it should be { bProp: string }
.