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

TS2531: Object is possibly 'null' but it is certain not null. switch(true) pattern. #41097

Closed
Artuar opened this issue Oct 14, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Artuar
Copy link

Artuar commented Oct 14, 2020

I tried to use switch(true) pattern in my code and found a bug.
There is a similar piece of code that shows the same bug.
Try it on the playground.

Playground Link:
TS Playground

Code

const switchTruePattern = (value1: number | null, value2: number | null) => {
    switch (true) {
        case value1 !== null && value2 !== null:
            return value1 + value2;
        case value1 !== null:
            return value1;
        default:
            return value2;
    }
};

TypeScript Version: 4.0.2

Expected behavior:
TS should correctly assume that next cannot be null after the conditional statement.

Actual behavior:
It throws TS2531: Object is possibly 'null'.

Related Issues:
#35964

@MartinJohns
Copy link
Contributor

Essentially a duplicate of #37178. Just the details vary slightly.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 15, 2020
@RyanCavanaugh
Copy link
Member

Please search a bit more diligently before logging new issues. Thanks!

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

3 participants