Closed
Description
TypeScript Version: 3.0 (current Playground)
Search Terms: Discriminated union falsy false
Code
interface True {
value: true;
trueString: string;
}
interface False {
value: false;
falseString: string;
}
type TrueOrFalse = True | False;
let x: TrueOrFalse;
if (!x.value) {
// DOES NOT COMPILE:
// Property 'falseString' does not exist on type 'TrueOrFalse'.
// Property 'falseString' does not exist on type 'True'.
x.falseString;
}
if (x.value) {
x.trueString;
} else {
// SAME ERROR AS ABOVE
x.falseString;
}
if (x.value === false) {
// WORKS
x.falseString;
}
if (x.value === true) {
x.trueString;
} else {
// WORKS
x.falseString;
}
Expected behavior:
Discriminated union is narrowed within falsy conditional blocks.
Actual behavior:
Discriminated union is not narrowed within (!falsy) conditional block, or within else
block of a (truthy) conditional.
Related Issues:
None found.