-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Typescript 3.8.3 : Sharp notation for private method must be alloweded #37677
Comments
i think their is not private method in your example. try to write #greet() to replace greet() method in your example, and you have this error message : "A method cannot be named with a private identifier.(18022)". |
True yeah, and I can't seem to find another issue about this 👍 |
The relevant proposal for private methods is here: https://github.com/tc39/proposal-private-methods Private methods are only shipping in XS so far, but they are in active development in V8. |
In the same way, static private attribute in class must be allowed too.
do :
Référence : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_fields |
This is a problem... class S {
static #instance: S;
static getInstance(): S {
if (!this.#instance) {
this.#instance = new S();
}
return this.#instance;
}
} How can I perform this? Thank you a lot in advance |
Private properties and methods should not be accessible from outside the class instance. Can be a constructor private? If it's not, is this singleton example valid? |
https://node.green/#ESNEXT-candidate--stage-3--static-class-fields-private-static-class-fields Node allow this way for creating singleton patterns. Try out yourself. I don't understand why TS is showing that error. |
It was a misunderstanding. I thought that your problem was the 'inaccessibility' of the private field Anyway. That's the point to use this pattern? Anyone can instantiate this class with |
That's a good point. I think we can use a private constructor. I think It fits well with this pattern. But, in a singleton pattern everyone can use getInstance method or new. If getInstance method exists, then should be used |
I'm also unable to use private getters and setters in a class in TS. class GSExample {
#a: number = 0;
get a() { // this is file
return this.#a;
}
set a(value: number) { // this is file
this.#a = value;
}
get #b() { // Error: An accessor cannot be named with a private identifier.(18023)
return this.a * 2;
}
set #b(value: number) { // Error: An accessor cannot be named with a private identifier.(18023)
this.a = value / 2;
}
} |
Is this possible in NodeJS? You can try in a Node console |
The class CExample {
#a: number = 0;
#constructor(a: number) { // Error: A method cannot be named with a private identifier.(18022)
this.#a = a;
}
} |
Not yet, unfortunately. proposal Staged 3 I have code pieces that use this feature (private getters and setters). They are transpiled by the Babel. Now I cannot rewrite them into TS. :( |
Then, If fails in TS is correct |
I mean private instance methods, private static methods, private accessor properties and private static accessor properties --- all this features are similar to each other and should be supported all together. |
NodeJS v12 have support for private class fields because it have V8 v7.4, which implements it even though stills at stage 3 |
While there is some confusion on this, both private fields and private methods are Stage 3. Private fields are allowed in TypeScript, but private methods are not (likely because the methods proposal took longer to get to Stage 3). In @denoland we have a version of V8 that supports the private methods proposal. It would be great to have private methods as part of |
QuickJS also has support for private methods: class Test {
#privateThing() {
console.log('Private');
}
publicThing() {
this.#privateThing();
}
} |
Private methods/accessors are now officially shipping in Chrome stable (https://chromestatus.com/feature/5700509656678400) and have been shipping in Node for a couple weeks (as of |
I think this should work class Queue {
#array = []
#isEmpty = () => {
return this.#array.length === 0;
}
} |
Yes, but I guess it leads to |
@orta is it really in discussion? It is a spec compliance issue now. |
Also, the workaround leads to the class having field initializers. |
One more annoying thing with the workaround: Forward references do not work. Works in ECMAScript:
Error ("Property '#init' is used before its declaration") when trying to represent this with the workaround in TypeScript, and also a run-time error in ECMAScript when trying to instantiate that class ("TypeError: Cannot read private member #init from an object whose class did not declare it"):
This means you are forced to define helper methods before fields, which might not be one's favorite code style. Considering all the downsides, in our project, we switched back to (clumsy) symbols / "soft" private members. But we are still eagerly waiting for TypeScript to support #private members and would immediately adapt our code! |
Is there any reason why it is not allowed to use private methods in Typescript? |
Good question. Especially when you use TSC only for type checking and do the actual compilation with Babel, apart from the error message TS18022, everything seems to work fine. |
That doesn't make any sense... This is a spec compliance issue and should just be fixed. |
Yet another issue is setting private identifiers via a class constructor: class Testing {
constructor(private #test: string) {} // ERROR: Private identifiers cannot be used as parameters
} |
Of course I would prefer a fix, too! |
Is this issue being addressed in the current roadmap? If not, has this issue been triaged at least? |
Good news here: #39066 (comment) |
Pls fix this soon, I don't use typescript but a ts linter and babel ts compilation and it sucks to have red lines everywhere while my code runs perfectly fine. |
I'm looking forward to fix, it's horrible microsoft/vscode#106351 |
Implemented in #42458 |
Search Terms
TS18022, sharp
Suggestion
Sharp notation for private method must be alloweded : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_instance_methods
Use Cases
class Test {
#test() {}
}
Examples
class Test {
#test() {}
}
=> TS18022: A method cannot be named with a private identifier.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: