Skip to content

Narrowed type of dependent parameter in destructured tuple union is widened by an inner loop #49772

@clo4

Description

@clo4

Bug Report

When narrowing the type of a dependent parameter of a tuple union by checking the discriminator, the type is no longer narrowed inside of, and after, a loop. This works correctly when destructuring an object, but not a tuple.

🔎 Search Terms

destructured union loop
destructured tuple union
array union type loop
destructured union dependent loop
dependent type loop

🕗 Version & Regression Information

  • This is the behavior in every version I tried (this feature was introduced in 4.6)

⏯ Playground Link

Playground link with relevant code

💻 Code

declare function fn(): [true, number] | [false, string];

const [guard, value] = fn();

value // type: string | number

if (guard) {
  value // type: number
  if (true) {
    value // type: number
  }
  for (;;) {
    value // type: string | number
  }
} else {
  value // type: string
  if (true) {
    value // type: string
  }
  while (0) {
    value // type: string | number
  }
}

🙁 Actual behavior

value is the correct type in the if statement, but is incorrect inside the loop and after it (no longer narrowed). It's assigned using const, so we know that this is wrong since the value can't change.

🙂 Expected behavior

value should be the same type inside the loop as it is outside the loop.

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