Skip to content

Missing parameter properties when using class with private fields #48771

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jlusty opened this issue Apr 19, 2022 · 4 comments · Fixed by #48775
Closed

Missing parameter properties when using class with private fields #48771

jlusty opened this issue Apr 19, 2022 · 4 comments · Fixed by #48775
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@jlusty
Copy link
Contributor

jlusty commented Apr 19, 2022

Bug Report

🔎 Search Terms

Parameter properties
Private fields
Class constructor
Destructuring assignment

🕗 Version & Regression Information

  • This changed between versions 4.5.5 and 4.6.2
  • Working in 4.6.0-dev.20220113
  • Broken in 4.6.0-dev.20220114, 4.6.0-beta and later (still broken on nightly)

I think this means the breaking change was made in #29374.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.6.0-dev.20220114#code/KYDwDg9gTgLgBAE2AMwIYFcA28DGnUDOBcAwvkQLICWRVAdgOYBiVwmCcA3gLABQcAuFGCoEEOpgCecAMRgoVAG6oYwFmwQAuOARgLGAbj59BcHON1R0OGNAAUJ04NRQG2znADWwSdsv0GOABfABpHJzgwdAAjTCocOFBIAmAEdXZtOnQAW2jgKHC4AEouQtM7D29fOBgACxoAOjkFZVV0jiC4AF44FwYio35BIOMhgUwIBjsSnjGnczoCCExgBompusbmpRU1VnYBssEFpZW1ybtNggakiBS0-YRDuZHeIKA

💻 Code

export default class ClassMissingField {
    readonly #privateField: string;

    constructor(
        arg: { key: string },
        public exposedField: number
    ) {
        ({ key: this.#privateField } = arg);
    }

    log() {
        console.log(this.#privateField);
        console.log(this.exposedField);
    }
}

🙁 Actual behavior

exposedField not set as a property on ClassMissingField, even though it should be via the parameter property. This is the minimal example I could find - it seems to need a private field with a destructuring assignment.

🙂 Expected behavior

exposedField should be set as a property on ClassMissingField using parameter properties.

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.7.1 milestone Apr 19, 2022
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 19, 2022
@RyanCavanaugh
Copy link
Member

@jakebailey this might be fixed by the other PR

@jakebailey
Copy link
Member

I just checked on main and this was unfortunately not fixed by either #48687 or #48765, no.

cc @JoshuaKGoldberg @patrickszmucer

@jakebailey
Copy link
Member

jakebailey commented Apr 19, 2022

This is pretty similar to those, though. Check out the emit of this: Playground Link

class ClassMissingFieldWithPrologue {
    constructor(arg, exposedField) {
        "prologue";
        "prologue";
        var _a;
        _ClassMissingFieldWithPrologue_privateField.set(this, void 0);
        (_a = this, { key: ({ set value(_b) { __classPrivateFieldSet(_a, _ClassMissingFieldWithPrologue_privateField, _b, "f"); } }).value } = arg);
    }
    log() {
        console.log(__classPrivateFieldGet(this, _ClassMissingFieldWithPrologue_privateField, "f"));
        console.log(this.exposedField);
    }
}
_ClassMissingFieldWithPrologue_privateField = new WeakMap();

Double prolougue, missing this.exposedField = exposedField.

This'd be a bug in classFields.ts, the thing that does the class fields transforms to lower ES versions.

@jlusty
Copy link
Contributor Author

jlusty commented Apr 20, 2022

I had a look into this and think I've found the issue - hopefully this is a fix: #48775

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants