facebook / facebook/flow

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

Aberta
#2,959 1 comentário 0 reações 0 responsáveis Ver no GitHub
feature request
Linguagem predominante
Rust
Estrelas
22.3k
Forks
1.9k
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.