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

in operator on T & object narrows to never #50639

Closed
OliverJAsh opened this issue Sep 5, 2022 · 4 comments Β· Fixed by #50666
Closed

in operator on T & object narrows to never #50639

OliverJAsh opened this issue Sep 5, 2022 · 4 comments Β· Fixed by #50666
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@OliverJAsh
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

  • in operator
  • generic
  • intersection
  • object
  • never

πŸ•— Version & Regression Information

  • This changed between versions 4.7 and 4.8

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const f3 = <A,>(value: A) => {
    if (
        typeof value === "object" &&
        value !== null &&
        "prop" in value
    ) {
        value;
        // ^?
        // TS 4.7: `A`
        // TS 4.8: `never`
        // Expected: `A & object`
    }
};

πŸ™ Actual behavior

See above.

πŸ™‚ Expected behavior

See above.


@erheron
Copy link

erheron commented Sep 6, 2022

It's quite interesting though, since if you remove the "prop" in value condition if, value is inferred as A & object as expected πŸ€”

@OliverJAsh
Copy link
Contributor Author

Note that even in TS 4.7 using the in operator on A & object returned never: https://www.typescriptlang.org/play?ts=4.7.4#code/MYewdgzgLgBAZgJhgXhgHgIIBoB8AKANwEMAbAVwFMAuGDGAMhhACMArC4KAShRxgG8AUDBEwAlnBh5ho2QCIADgCcQCueLAxi5CjJE8hs2dsoBuPUYD0lmAD0A-BYC+gp6aA. This only became an issue after TS 4.8 started narrowing A to A & object.

@fatcerberus
Copy link

Since this involves narrowing via in, this might be related to #50531.

@andrewbranch
Copy link
Member

Yes, related to #50531, but not going to be fixed for 4.8.3. 4.8.3 will contain a more minimal fix specific to unknown and {}, while for 4.9 we plan to fix the other examples you show here. @ahejlsberg is actively working on this. I’m not sure there’s an existing issue tracking it though, so I guess this is it now.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 6, 2022
@andrewbranch andrewbranch added this to the TypeScript 4.9.0 milestone Sep 6, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants