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

Type narrowing not working as expected in else #26852

Open
Tyriar opened this issue Sep 2, 2018 · 5 comments
Open

Type narrowing not working as expected in else #26852

Tyriar opened this issue Sep 2, 2018 · 5 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@Tyriar
Copy link
Member

Tyriar commented Sep 2, 2018

For the following code with strictNullChecks enabled:

interface Base {
    a: {} | null;
    b: {} | null;
}

function test(base: Base): void {
    if (!base.a && !base.b) {
        const result: null = null;
    } else if (!base.a && base.b) {
        const result: {} = base.b;
    } else if (base.a && !base.b) {
        const result: {} = base.a;
    } else {
        const result: {} = base.b;
    }
}

I'm seeing this error on the last result:

Type '{} | null' is not assignable to type '{}'.
  Type 'null' is not assignable to type '{}'.

I expect it to work as the else implies:

   !(!base.a && !base.b) && !(!base.a && base.b) && !(base.a && !base.b) 
 = (base.a || base.b) && (base.a || !base.b) && (!base.a || base.b)
 = (base.a || base.b) && (base.a) && (base.b)
      note: !base.b and !base.a are canceled out as the expression cannot be true if one
            of them is false
 = base.a && base.b

TS play link

@mattmccutchen
Copy link
Contributor

TypeScript narrowing doesn't do arbitrary Boolean algebra; it just looks for conjuncts of specific forms in the path condition of each path to the operation of interest. You could turn this issue into a suggestion, but I'm guessing it will be declined on performance grounds. Or you could just rewrite the code as:

function test(base: Base): void {
    if (!base.b) {
        if (!base.a) {
            const result: null = null;
        } else { 
            const result: {} = base.a;
        }
    } else { 
        const result: {} = base.b;
    }
}

@ghost ghost added the Question An issue which isn't directly actionable in code label Sep 3, 2018
@Tyriar
Copy link
Member Author

Tyriar commented Sep 3, 2018

@mattmccutchen makes sense, it's a suggestion/feature request then. It seems like something that could make type narrowing even more powerful than it is, I'm not sure what the performance challenges are though.

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@Tyriar
Copy link
Member Author

Tyriar commented Jul 13, 2019

@DanielRosenwasser I think this is labelled incorrectly and shouldn't have been closed, it never really got an answer on why or why not from a maintainer.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript and removed Question An issue which isn't directly actionable in code labels Jul 13, 2019
@DanielRosenwasser
Copy link
Member

Sorry, I don't know why this was labeled as a question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants