Skip to content

Error on narrowing down non-null variables #53064

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

Closed
LaurentRos opened this issue Mar 2, 2023 · 2 comments
Closed

Error on narrowing down non-null variables #53064

LaurentRos opened this issue Mar 2, 2023 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@LaurentRos
Copy link

Bug Report

πŸ”Ž Search Terms

typenarrowing, null

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about null and narrowing

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A = {
    c: null
    d: string
} | {
    c: string
    d: null
} | {
    c: string
    d: string
}

const hello = (a: A): void => {
    console.log(a.c)
    console.log(a.d)
}

const main = (c: string | null, d: string | null): void => {
    if (!c && !d) {
        return;
    }

    if (!!c && !d) {
        return hello ({c, d})
    }

    if (!c && !!d) {
        return hello ({c, d})
    }

    return hello ({c, d})
}

πŸ™ Actual behavior

Typescript doesn't narrow down the variables to be non-null at the end
image

πŸ™‚ Expected behavior

Typescript should be able to narrow down the types to both be non-null

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Mar 2, 2023
@RyanCavanaugh
Copy link
Member

Narrowing is based on the parent control flow graph introducing a direct narrowing, rather than doing a sort of constraint-solving process as would be required to figure this one out. We don't intend to switch to the latter method since code that requires that sort of analysis is quite rare in practice.

@fatcerberus
Copy link

fatcerberus commented Mar 2, 2023

#52822 (comment) feels relevant here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants