Skip to content

Downlevel for (this.#x of ...) / for (this.#x in ...) emits __classPrivateFieldGet(...) as the loop target #65413

Open

Description

@trackoor

🔎 Search Terms

private field for-of downlevel, for-in private name, __classPrivateFieldGet for-of, Invalid left-hand side in for-loop, es2021 private field loop

🕗 Version & Regression Information

Not a regression. Only affects targets that lower #private (es2021 and below); --target es2022 is correct.

  • Fails: 5.9.3, 6.0.3, 7.0.1-rc, 7.0.2, 7.1.0-beta, 7.1.0-dev.20261106.1, 7.1.0-dev.20261109.1

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3&target=4#code/PTAEAEBcEMCcHMCmkBcoCiBlATABgIwDsAUAMYA20AzlaAIKgDexooAxAB5rQB2AnqAC8oXAG4WoWAFceACgD2aeQCMAVolKQAlEwmsAZvNihZkABYBLKgDpOoeftABtfPgC6OxgF89oQ8dNLGzsLHntPH1ZWWGQpWDDzK1sOcVYfH1J5Hip5ckRrcnl4WR5EAHd6WS1raTlGUABrNHxQLy0tcSA

(Playground, 6.0.3, target ES2017: the .JS tab shows for (__classPrivateFieldGet(this, _A_x, "f") of [11]) { } with 0 errors. The 7.1 nightly in Playground v2 emits the same.)

💻 Code

class A {
  #x: any = 0;
  run(o: object) {
    for (this.#x of [11]) {}
    for (this.#x in o) {}
    return this.#x;
  }
}
console.log(new A().run({ k: 1 }));

🙁 Actual behavior

tsc --target es2017 repro.ts reports nothing and emits:

        for (__classPrivateFieldGet(this, _A_x, "f") of [11]) { }
        for (__classPrivateFieldGet(this, _A_x, "f") in o) { }

node repro.js fails to load the file:

ReferenceError: Invalid left-hand side in for-loop

🙂 Expected behavior

A valid assignment target, and node repro.js prints k as it does with --target es2022. For destructuring ([this.#x] = [11]) tsc already emits ({ set value(_b) { __classPrivateFieldSet(...); } }).value; the for...in/for...of initializer, when it is not a variable declaration, needs the same treatment.

Additional information about the issue

Private accessors (set #x(v)) behave the same way. The whole module fails to parse, so nothing in the file runs.


This was found with an AI-assisted differential testing tool; the repros and outputs above were re-run on the listed versions.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions