Flow: Support private methods

Created on 6 May 2018  路  11Comments  路  Source: facebook/flow

Most helpful comment

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?

All 11 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

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

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

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));
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebmck picture sebmck  路  113Comments

sophiebits picture sophiebits  路  66Comments

jlongster picture jlongster  路  55Comments

TylerEich picture TylerEich  路  49Comments

NgoKnows picture NgoKnows  路  40Comments