-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Support private methods #6254
Comments
Any update about this? When trying to use private methods ( #myFunction(){} ) I got this warning: Classes may not have private methods. I'm using Flow 0.93. |
I'm interested in this feature as well. I have classes that extend other classes and using the typical ES6 approaches to "private" attributes/methods (like #, WeakMap, Symbols, etc.) just don't work out in my architecture. If flowtype handled this itself, it would be much easier (which means possibly adding a "protected" modifier as well) |
@jml6m I think TypeScript better suits your use case, Flow wouldn't add protected, because it isn't standard js |
Private methods is already in stage 3 so it will be standard soon. |
They meant that "protected" class fields isn't standard JS :) |
Any news on this? Why is every second issue in this repository dead since end 2019? Why is there no global setting to suppress specific errors? |
This is the main reason we're trying to migrate to typescript. I wish I could help here. |
So jul 30 2020. Any news on this issue? |
Yes, please! Since private properties already work, it should be trivial to add support for private methods as well. And this is already Stage 3 standard, as is mentioned above, so there's no reason to not do it. |
Meanwhile, one way to go around it is to simply use interfaces. interface Greeter
{
getGreeting(): string
}
class MyGreeter implements Greeter
{
getGreeting(): string
{
return 'Hello, ' + this.getName() + '!'
}
#getName(): string
{
return 'World'
}
}
function greet(greeter: Greeter): void
{
console.log(greeter.greet());
}
let greeter = new MyGreeter()
greet(greeter) The function |
Another workaround is treating it like a property (assigning it) and then using class Foo {
#priv: (number) => string;
constructor(){
this.#priv = (a) => String(a);
}
run(){
console.log(this.#priv.call(this, 123));
}
} |
Summary: This diff adds support for type checking private methods. I added private methods and private static methods into the same class entries that stores type signature of private fields, and then use these type signature information to resolve `GetPrivatePropT` and `PrivateMethodT`. I also ensured that unbinding private methods are also correctly errored on, similar to unbound public methods. Fixes #6254 #4872 #7877. Reviewed By: jbrown215 Differential Revision: D30023778 fbshipit-source-id: 71fae1a81d9b373f18b9ec4743380e846564a49f
https://github.com/tc39/proposal-private-methods
The text was updated successfully, but these errors were encountered: