Flow: Private field can't be called as a function

Created on 2 Jul 2019  路  4Comments  路  Source: facebook/flow

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.

object model bug

Most helpful comment

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

All 4 comments

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

Any news?

As a temporary solution, do this:

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

instead of:

this.#foo(/* args */)

Still not work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

funtaps picture funtaps  路  3Comments

marcelbeumer picture marcelbeumer  路  3Comments

philikon picture philikon  路  3Comments