Skip to content

Nested destructuring does not narrow dependent parameters #48378

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

Open
Fireboltofdeath opened this issue Mar 22, 2022 · 2 comments
Open

Nested destructuring does not narrow dependent parameters #48378

Fireboltofdeath opened this issue Mar 22, 2022 · 2 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@Fireboltofdeath
Copy link

Bug Report

πŸ”Ž Search Terms

destructuring destructured dependent parameters nested control flow analysis

πŸ•— Version & Regression Information

  • This changed between versions 4.5.5 and 4.6

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare const Tuple: [true, string] | [false, number];
const [[val1, val2]] = [Tuple];
if (val1) {
	val2 // string | number
}

πŸ™ Actual behavior

TypeScript does not correctly narrow val2 when narrowing val1 if it's in any type of nested destructuring.

πŸ™‚ Expected behavior

I expect it to behave the same as directly destructuring the object.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Mar 28, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 28, 2022
@jtbandes
Copy link
Contributor

Is this the same issue?

type T = { path: ['one'], value: string } | { path: ['two'], value: number }

function foo(payload: T) {
  if (payload.path[0] === 'one') {
    payload.value  // string | number, should be just string
  }
}

@Fireboltofdeath
Copy link
Author

This issue is referring to the control flow analysis of destructured unions so I don't believe this is related to that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants