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

Wrong type narrowing with Boolean(expression) #37770

Closed
Felix-Indoing opened this issue Apr 3, 2020 · 2 comments
Closed

Wrong type narrowing with Boolean(expression) #37770

Felix-Indoing opened this issue Apr 3, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Felix-Indoing
Copy link

Felix-Indoing commented Apr 3, 2020

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

@IllusionMH
Copy link
Contributor

Duplicate of #16655

Unfortunately changing Boolean declaration breaks cases with filter etc.
See #29955

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 3, 2020
@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

4 participants