Skip to content

in operator on T & object narrows to never #50639

Closed

Description

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.


Activity

  1. erheron commented on Sep 6, 2022

    @erheron

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

  2. OliverJAsh commented on Sep 6, 2022

    @OliverJAsh
    ContributorAuthor

    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.

  3. fatcerberus commented on Sep 6, 2022

    @fatcerberus

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

  4. andrewbranch commented on Sep 6, 2022

    @andrewbranch
    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. Anders Hejlsberg (@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.

  5. added
    Fix Available
    on Sep 7, 2022
  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFix Available

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions