facebook / facebook/flow

Returning an instance of callee's class with different generic constraint fails

Open
#2,959 1 comment 0 reactions 0 assignees View on GitHub
feature request
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

As far as I understand there's no way to express that a method should return an instance of callee's class but with different generic parameter. It is possible to override it in type definitions but it has also another implication.

It could have a big impact on subclassing of generic classes. If there was a way to do this, subclasses wouldn't have duplicate definitions with tweaked types. Big win for readability and maintainability of definitions.

I'm sorry if this is a duplicate (I'm almost sure it is) but I couldn't find anything relevant.

---

Example:

```js
class A {
name: T
constructor(name: T) {
this.name = name
}

method(f: (from: T) => U) {
return new this.constructor(f(this.name))
}
}
```

fails with

```js
test.js:10
10: return new this.constructor(f(this.name))
^^^^^^^^^^^^ U. This type is incompatible with the expected param type of
3: class A {
^ some incompatible instantiation of `T`

Found 1 error
```

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.