facebook / facebook/flow

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

Aperta
#2,959 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
feature request
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.