Skip to content

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

Closed
@adalinesimonian

Description

@adalinesimonian

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

Assignees

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