Skip to content

Error on narrowing down non-null variablesΒ #53064

Closed
@LaurentRos

Description

@LaurentRos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions