facebook / facebook/flow

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

Offen
#2,959 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
feature request
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.