facebook / facebook/flow

Incorrect constructor detected when using a Function in the left side of `extends`

Open
#4,331 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

When a function call is used as the left side of an class extends expression, Flow does not correctly detect the constructor based on the function result. Here is an example that demonstrates the problem:

```javascript
//@flow

class OneArgClass {
/*::
argA: string
*/
constructor (argA) {
this.argA = argA;
}
}

class TwoArgClass {
/*::
argB: string
argC: string
*/
constructor (argB, argC) {
this.argB = argB;
this.argC = argC;
}
}

const FunctionMixin = (Base) => class extends Base {

notInteresting () {
console.log('Do nothing');
}
};

class OneArgSubclass extends FunctionMixin(OneArgClass) {

}

class TwoArgSubclass extends FunctionMixin(TwoArgClass) {

}

const oneArgInstance = new OneArgSubclass('argA');
const twoArgInstance = new TwoArgSubclass('argB', 'argC');
```

When that code is checked by flow, the following error is shown:

```
index.js:38
38: const oneArgInstance = new OneArgSubclass('argA');
^^^^^^^^^^^^^^^^^^^^^^^^^^ constructor call
19: this.argC = argC;
^^^^ undefined (too few arguments, expected default/rest parameters). This type is incompatible with
19: this.argC = argC;
^^^^^^^^^ string

index.js:39
39: const twoArgInstance = new TwoArgSubclass('argB', 'argC');
^^^^^^ unused function argument
v-------------------
7: constructor (argA) {
8: this.argA = argA;
9: }
^ function expects no more than 1 argument

Found 2 errors
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.