facebook / facebook/flow

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

Ouverte
#2,959 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
feature request
Langage dominant
Rust
Étoiles
22.3k
Forks
1.9k
Métriques de merge des PR
Aucune PR mergée en 30 j

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
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.