Skip to content

Wrong type narrowing with Boolean(expression) #37770

@Felix-Indoing

Description

@Felix-Indoing

TypeScript Version: 3.8.3

Search Terms: Boolean type narrowing

Code

    const x: {test: string} | null | undefined = something()

    // this works
    if (!!x) {
        console.log(x.test)
    }

    // this can not work
    if (Boolean(x)) {
        console.log(x.test)
    }

Expected behavior:
Expression inside the second if statement should work.

Actual behavior:
The second if statement gives the following error:

TS2533: Object is possibly 'null' or 'undefined'.

Related Issues: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions