Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discriminated union does not narrow based on falsy conditional #27059

Closed
kevinder opened this issue Sep 12, 2018 · 2 comments
Closed

Discriminated union does not narrow based on falsy conditional #27059

kevinder opened this issue Sep 12, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kevinder
Copy link

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.

Playground Link:
http://www.typescriptlang.org/play/#src=interface%20True%20%7B%0D%0A%20%20%20%20value%3A%20true%3B%0D%0A%20%20%20%20trueString%3A%20string%3B%0D%0A%7D%0D%0Ainterface%20False%20%7B%0D%0A%20%20%20%20value%3A%20false%3B%0D%0A%20%20%20%20falseString%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Atype%20TrueOrFalse%20%3D%20True%20%7C%20False%3B%0D%0Alet%20x%3A%20TrueOrFalse%3B%0D%0A%0D%0Aif%20(!x.value)%20%7B%0D%0A%20%20%20%20%2F%2F%20DOES%20NOT%20COMPILE%3A%0D%0A%20%20%20%20%2F%2F%20Property%20'falseString'%20does%20not%20exist%20on%20type%20'TrueOrFalse'.%0D%0A%20%20%20%20%2F%2F%20%20%20Property%20'falseString'%20does%20not%20exist%20on%20type%20'True'.%0D%0A%20%20%20%20x.falseString%3B%0D%0A%7D%0D%0A%0D%0Aif%20(x.value)%20%7B%0D%0A%20%20%20%20x.trueString%3B%0D%0A%7D%20else%20%7B%0D%0A%20%20%20%20%2F%2F%20SAME%20ERROR%20AS%20ABOVE%0D%0A%20%20%20%20x.falseString%3B%0D%0A%7D%0D%0A%0D%0Aif%20(x.value%20%3D%3D%3D%20false)%20%7B%0D%0A%20%20%20%20%2F%2F%20WORKS%0D%0A%20%20%20%20x.falseString%3B%0D%0A%7D%0D%0A%0D%0Aif%20(x.value%20%3D%3D%3D%20true)%20%7B%0D%0A%20%20%20%20x.trueString%3B%0D%0A%7D%20else%20%7B%0D%0A%20%20%20%20%2F%2F%20WORKS%0D%0A%20%20%20%20x.falseString%3B%0D%0A%7D%0D%0A

Related Issues:
None found.

@DanielRosenwasser
Copy link
Member

Works in strictNullChecks, and so this is a duplicate of #10564.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Sep 13, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants