Skip to content

Argument type refinement lost in binary expression #48483

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
thers opened this issue Mar 30, 2022 · 2 comments
Closed

Argument type refinement lost in binary expression #48483

thers opened this issue Mar 30, 2022 · 2 comments

Comments

@thers
Copy link

thers commented Mar 30, 2022

Bug Report

πŸ”Ž Search Terms

Type refinement, binary expression, logical or

πŸ•— Version & Regression Information

This is the behavior in every version I tried (didn't test below v4 though), including nightly.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface SomeObj {
    foo: string,
    bar: number,
}

function test_INCORRECT(obj: SomeObj | undefined) {
    if (!obj) {
        return null;
    }

    return {
        foo: obj.foo,
        baz: true || (obj.bar === 42),
    };
}

πŸ™ Actual behavior

A TS2532 Object is possibly 'undefined' error is rised for obj in true || (obj... part of code, despite the fact that obj was refined to be exactly SomeObj.

πŸ™‚ Expected behavior

No error for this part of code.

@ajafff
Copy link
Contributor

ajafff commented Mar 30, 2022

That's because the code after true || is unreachable. TypeScript doesn't do any type narrowing in unreachable code.

Therefore this is a duplicate of #26914

@thers
Copy link
Author

thers commented Mar 30, 2022

Thanks for linking that issue, I wasn't able to find anything related as just searching for true || term gives you 300+ pages of issues!

@thers thers closed this as completed Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants