Skip to content

Erroneous generated JS code when incrementing private (hashtag) fields with parenthesis #49935

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

Closed
mfederczuk opened this issue Jul 17, 2022 · 0 comments · Fixed by #50111
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@mfederczuk
Copy link

Bug Report

When incrementing a private class field (using the hashtag-syntax) and the field is put inside parenthesis while the increment operator is outside the parenthesis, invalid JavaScript code is generated.
e.g.: ++(this.#n)

🔎 Search Terms

private hashtag field increment parenthesis

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about private class fields and incrementing/decrementing

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo {
  #n: number = 0;

  inc() {
    ++(this.#n); // generated JS code here is erroneous
  }
}

const foo = new Foo();
foo.inc();

🙁 Actual behavior

The following code is generated:

++(__classPrivateFieldGet(this, _Foo_n, "f"))

which leads to the error: invalid increment/decrement operand when executing it, since the result of a function cannot be incremented/decremented.

The compiler seems to think that the variable-reference expression is read-only because of the parenthesis.

🙂 Expected behavior

The parenthesis should be ignored by the compiler and the correct code needs to be generated:

__classPrivateFieldSet(this, _Foo_n, (_a = __classPrivateFieldGet(this, _Foo_n, "f"), ++_a), "f")
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
4 participants