Skip to content

4.4 regression: in operator does not narrow types in generic constraints when destructuring #45762

Description

Bug Report

🔎 Search Terms

in operator, narrowing, generics, constraints, destructuring

🕗 Version & Regression Information

  • This changed between versions 4.3.5 and 4.4.2

⏯ Playground Link

Playground link with relevant code

💻 Code

type X = { a: string } | { b: string }

// Works in 4.3.5 and 4.4.2
function func1<T extends X>(value: T) {
  if ('a' in value) {
    const a = value.a
    console.log('a', a)
  } else {
    const b = value.b
    console.log('b', b)
  }
}

// Works in 4.3.5, not in 4.4.2
function func2<T extends X>(value: T) {
  if ('a' in value) {
    const { a } = value // Property 'a' does not exist on type 'X'.(2339)
    console.log('a', a)
  } else {
    const { b } = value // Property 'b' does not exist on type 'X'.(2339)
    console.log('b', b)
  }
}

🙁 Actual behavior

Types are not narrowed when using destructuring assignment and the compiler throws error 2339.

🙂 Expected behavior

Types are narrowed when using destructuring assignment.

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions