Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
adalinesimonian opened this issue Sep 7, 2021 · 3 comments Β· Fixed by #47337
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@adalinesimonian
Copy link

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.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 7, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.1 milestone Sep 7, 2021
@andrewbranch
Copy link
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.

@a-tarasyuk
Copy link
Contributor

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

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

@andrewbranch
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants