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

Private field can't be called as a function #7877

Closed
noppa opened this issue Jul 2, 2019 · 5 comments
Closed

Private field can't be called as a function #7877

noppa opened this issue Jul 2, 2019 · 5 comments

Comments

@noppa
Copy link
Contributor

noppa commented Jul 2, 2019

Flow version: 0.102.0

Expected behavior

The following should typecheck without errors.

type Fn = () => any

class A {
  #foo: Fn = () => 42
  callFoo() {
    this.#foo()
  }
}

Actual behavior

Flow gives an error

    6:     this.#foo()
                ^ Cannot call `this.#foo` because property `foo` is missing in `A` [1].
        References:
        3: class A {
                 ^ [1]

Changing the call site to

    const foo = this.#foo
    foo()

works as expected.

Issue #6254 is related, but seems to be more about defining private methods, which is a separate ES proposal. This issue is more about using private fields that can already be defined just fine.

  • Link to Try-Flow or Github repo:

https://flow.org/try/#0C4TwDgpgBAYgdlAvFAFASiQPigQziAKAIGMAbHAZwqgEEoBvAqKAYgDMB7DgLlgWXRYoAFgBMTKMRylSMLoMbNmwABYBLCgDp28tBIC+BfUA.

@mvitousek
Copy link
Contributor

Good find, I'm working on a fix to this now.

rk-for-zulip added a commit to rk-for-zulip/zulip-mobile that referenced this issue Dec 12, 2019
The TC39 class fields proposal [1] -- stage 3, but already implemented
and available in Chrome 74 -- allows for truly private member
variables. This feature has also been supported by Babel since at
least version 7.3.

Alas, the intersection of private fields and *Flow* is a much more
poorly-supported area. There are several known issues:

* prettier/prettier#5288
* babel/babel-eslint#688
* facebook/flow#7877

A workaround exists for the first one (Babel is now usable as the
parser), but not the latter two.

As an approximation, then, we allow leading underscores in property
names until this feature is available.

[1]: https://github.com/tc39/proposal-class-fields
gnprice pushed a commit to rk-for-zulip/zulip-mobile that referenced this issue Dec 13, 2019
The TC39 class fields proposal [1] -- stage 3, but already implemented
and available in Chrome 74 -- allows for truly private member
variables. This feature has also been supported by Babel since at
least version 7.3.

Alas, the intersection of private fields and *Flow* is a much more
poorly-supported area. There are several known issues:

* prettier/prettier#5288
* babel/babel-eslint#688
* facebook/flow#7877

A workaround exists for the first one (Babel is now usable as the
parser), but not the latter two.

As an approximation, then, we allow leading underscores in property
names until this feature is available.

[1]: https://github.com/tc39/proposal-class-fields
@xdevs23
Copy link

xdevs23 commented Mar 30, 2020

Any news?

@xdevs23
Copy link

xdevs23 commented Mar 30, 2020

As a temporary solution, do this:

this.#foo.call(this, /* args */)

instead of:

this.#foo(/* args */)

@Artexoid
Copy link

Still not work.

facebook-github-bot pushed a commit that referenced this issue Aug 17, 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: 5575306de4ac504a92fb8bba221221566b5ac52d
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
@noppa
Copy link
Contributor Author

noppa commented Aug 25, 2021

This has been fixed in version 0.158.0.

@noppa noppa closed this as completed Aug 25, 2021
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

5 participants