Repository navigation
Closed
Labels
DuplicateAn existing issue was already created
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.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already created
Activity
DanielRosenwasser commentedon Sep 13, 2018
Works in
strictNullChecks, and so this is a duplicate of #10564.typescript-bot commentedon Dec 14, 2018
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.