Skip to content

ArrayLiteral instead of ArrayBindingPattern as loop variable #61853

Closed
@knutwannheden

Description

@knutwannheden

🔎 Search Terms

"for loop", "array literal", "array binding pattern"

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about for loops
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250612#code/DYUwLgBAZglgTgZzAGggkBjA9gOwCYDcAUFFnBABQDasiKamueAuhFlBFVQIzIBMzZFQDMyACzNmASggBvIhEURsOBFlAA6YFgDmFWklToVeKcQC+QA

💻 Code

let first, second;
for ([first, second] of [[1,2],[3,4]]) {
    console.log(first, second);
}

🙁 Actual behavior

The [first, second] expression in the example's for ... of loop is mapped to a ts.ArrayLiteral which gets assigned to the ts.ForOfStatement's initalizer property.

🙂 Expected behavior

Even though the variables are declared before the loop, I would have expected to see a ts.ArrayBindingPattern node in the AST.

Additional information about the issue

See also: https://ts-ast-viewer.com/#code/DYUwLgBAZglgTgZzAGggkBjA9gOwCYDcAUFFnBABQDasiKamueAuhFlBFVQIzIBMzZFQDMyACzNmASggBvIhEURsOBFlAA6YFgDmFWklToVeKcQC+QA

The same is true for object binding patterns, which get mapped to ts.ObjectLiteralExpressions rather than a ts.ObjectBindingPattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions