Skip to content

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

Closed

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.

Activity

  1. andrewbranch commented on Sep 7, 2021

    @andrewbranch
    Member

    I’d start by making sure getNarrowableTypeForReference is returning X rather than T for the reference value, since that’s what 4.3 added that makes this possible in the first place.

  2. a-tarasyuk commented on Sep 12, 2021

    @a-tarasyuk
    Contributor

    I’d start by making sure getNarrowableTypeForReference is returning X rather than T

    Andrew Branch (@andrewbranch) It seems this commit 0cc5c7b changed getNarrowableTypeForReference behavior to returning T rather than X for binding patterns.

  3. andrewbranch commented on Sep 13, 2021

    @andrewbranch
    Member

    I subsequently killed more contextual typing by binding patterns in #45719, not sure at a glance whether that will play a role here or not

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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