Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@adalinesimonian

Description

@adalinesimonian
Issue body actions

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.

Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.