Skip to content

Incorrect generated JS code when private (hashtag) property is used with parenthesis in left assignment #53347

@jbl428

Description

@jbl428

Bug Report

When private (hashtag) property is used with parenthesis in left assignment, TS generate __classPrivateFieldGet, not __classPrivateFieldSet.

I think this issue is related with this one.
It should apply __classPrivateFieldSet only if there is incrementing/decrementing operations.

🔎 Search Terms

  • __classPrivateFieldGet
  • private property
  • Invalid left-hand side in assignment

🕗 Version & Regression Information

  • If compile target is ES2022, it is not an issue.
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about setting private property.

⏯ Playground Link

Playground link with relevant code

💻 Code

class Temp {
  readonly #id: number;

  constructor(value: number) {
    this.#id = value;
  }

  setId(id: number) {
    (this.#id as number) = id;
    // (this.#id) = id <-- this one is also problem.
  }
}

new Temp(10).setId(20)

🙁 Actual behavior

TS generate the following code.

class Temp {
    constructor(value) {
        _Temp_id.set(this, void 0);
        __classPrivateFieldSet(this, _Temp_id, value, "f");
    }
    setId(id) {
        __classPrivateFieldGet(this, _Temp_id, "f") = id; // it will throw error `Invalid left-hand side in assignment`
    }
}

🙂 Expected behavior

TS should generate the following code.

class Temp {
    constructor(value) {
        _Temp_id.set(this, void 0);
        __classPrivateFieldSet(this, _Temp_id, value, "f");
    }
    setId(id) {
        __classPrivateFieldSet(this, _Temp_id, id, "f");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions