Skip to content
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

JavaScript: access to private static field in class method not recognized as valid #56326

Closed
bobnil opened this issue Nov 6, 2023 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@bobnil
Copy link

bobnil commented Nov 6, 2023

Type: Bug

The JavaScript syntax verifier considers access to private static properties in class methods to be illegal.

JavaScript example:

class Example {
  static #privateProperty = 'private example';
  static publicProperty = 'public example';
  constructor() {
    alert(this.constructor.publicProperty);
    alert(this.constructor.#privateProperty); // Considered to be illegal
  }
}

new Example();

The alert uses the static properties of the class inside a class method.
Both the access to the publicProperty and the #privateProperty works when running the code (tested in Chrome, Edge, and Firefox), but the IntelliSense isn't aware of this syntax: It doesn't give any suggestions of possible values.

The editor considers the access to #privateProperty to be illegal, since it starts with a #.

The editor doesn't complain about the access to publicProperty since this kind of access has been allowed for ages.

VS Code version: Code 1.84.0 (d037ac076cee195194f93ce6fe2bdfe2969cc82d, 2023-11-01T11:29:04.398Z)
OS version: Windows_NT x64 10.0.19045

@mjbvz mjbvz transferred this issue from microsoft/vscode Nov 6, 2023
@fatcerberus
Copy link

Actually both are considered errors: Playground

Property 'publicProperty' does not exist on type 'Function'.(2339)

The underlying issue being that .constructor is typed as Function rather than typeof Example. Relevant GitHub issue for that is #3841

@bobnil
Copy link
Author

bobnil commented Nov 6, 2023

@fatcerberus Yes, that seams to be the problem.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 6, 2023
@vadimmos
Copy link

vadimmos commented Nov 7, 2023

This approach helped me playground

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants