facebook / facebook/flow

Getter and setter variances change depending on whether both are present

Abierto
#3,984 0 comentarios 0 reacciones 0 asignados Ver en GitHub
bug Typing: property variance
Lenguaje dominante
Rust
Estrellas
22.3k
Forks
1.9k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I've recently discovered that Flow's getter/setter polarities vary counter-intuitively. When only a getter or only a setter is present, then Flow takes the corresponding field as covariant or contravariant, respectively. When both a getter and a setter are present, however, Flow takes the field as invariant. Since Flow currently has no notation (that I know of) to specify an invariant getter or setter, an extension class cannot complete the pair for an older class, e.g.

```js
class A {
// `f` is covariant
get f(): number { return 0; }
}
class B extends A {
// this is an error because I've overridden a covariant field with a contravariant one
set f(n: number) { console.log(n); }
}
```

This seems like bad manners. Ultimately, this behavior traces to `Type.Property.polarity`, [src/typing/type.ml#L990-992](https://github.com/facebook/flow/blob/4b126172b0e3ec7e581314d7f9887934b6079b99/src/typing/type.ml#L990-L992). I've noticed one place that implicitly depends on the current behavior, [src/typing/flow_js.ml#L7513-L7519](https://github.com/facebook/flow/blob/4b126172b0e3ec7e581314d7f9887934b6079b99/src/typing/flow_js.ml#L7513-L7519); it has an analogue under `set_prop`. Any plans to shift getters and setters to uniformly invariant, where the `+` and `-` variance operators admit overrides?

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.