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

Support private methods #6254

Closed
vjpr opened this issue May 6, 2018 · 11 comments
Closed

Support private methods #6254

vjpr opened this issue May 6, 2018 · 11 comments

Comments

@vjpr
Copy link

vjpr commented May 6, 2018

https://github.com/tc39/proposal-private-methods

@hadnet
Copy link

hadnet commented Mar 28, 2019

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.

@jml6m
Copy link

jml6m commented Jun 14, 2019

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)

@goodmind
Copy link
Contributor

@jml6m I think TypeScript better suits your use case, Flow wouldn't add protected, because it isn't standard js

@sakarit-zz
Copy link

@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.

@ephys
Copy link

ephys commented Oct 23, 2019

Private methods is already in stage 3 so it will be standard soon.

They meant that "protected" class fields isn't standard JS :)

@nnmrts
Copy link
Contributor

nnmrts commented May 30, 2020

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?

@sepehr
Copy link

sepehr commented May 30, 2020

This is the main reason we're trying to migrate to typescript. I wish I could help here.

@hadnet
Copy link

hadnet commented Jul 30, 2020

So jul 30 2020. Any news on this issue?

@XedinUnknown
Copy link

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.

@XedinUnknown
Copy link

XedinUnknown commented Feb 11, 2021

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 greet() doesn't know that greeter is an instance of MyGreeter; all it knows is that it's a Greeter. This is DIP, and I do this anyway in all my code.

@mischnic
Copy link
Contributor

Another workaround is treating it like a property (assigning it) and then using .call

class Foo {
    #priv: (number) => string;

    constructor(){
        this.#priv = (a) => String(a);
    }

    run(){
        console.log(this.#priv.call(this, 123));
    }
}

facebook-github-bot pushed a commit that referenced this issue Aug 18, 2021
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants