Consider the following code (try):
class C {
constructor() {
this.method = this.method.bind(this);
}
//method: () => void; // uncomment this line
method() {}
}
Which generates the following error:
3: this.method = this.method.bind(this);
^ property `method`. Covariant property `method` incompatible with contravariant use in
3: this.method = this.method.bind(this);
^ assignment of property `method`
As noted in the code, if you add a separate property type declaration, the error is gone. IMO, it does not really make sense to enforce a separate declaration here as the method definition should be enough?
Methods are covariant, so that they can be overridden with a different but compatible signature in subclasses, but this also means that you can't assign to them, since you have no way of knowing what the concrete method signature really is and whether what you're assigning will be compatible.
The method: () => void line defines a property, which is invariant, so you can assign to it, but when a subclass calls it they will always see the () => void signature and not the signature of their own override (if any).
See https://flowtype.org/blog/2016/10/04/Property-Variance.html
seems like you need to add the function definition as type:
https://github.com/ryyppy/flow-guide/issues/6
Got the same problem, solved by the long form syntax:
method = function() {
I'm going to close this, works as expected
just for the record, what @psam44 mentioned is not the long form syntax (mentioned in the flow blog post) but an arrow function. Arrow functions are not present on the object's prototype, they are merely properties holding a reference to a function.
Most helpful comment
Got the same problem, solved by the long form syntax:
method = function() {