facebook / facebook/flow

Better error messages for incompatibilities due to variance

Open
#8,041 1 comment 0 reactions 1 assignee Claimed by @SamChou19815 View on GitHub
discussion error messages
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

## Proposal

```js
class Animal {}
class Cat extends Animal {}

class Cont {}

(new Cont : Cont)
```

The above example currently displays the following error:

```
Cannot cast `new Cont<...>()` to `Cont` because `Cat` [1] is incompatible with `Animal` in type argument `T`
```

For many people, it is quite an unintuitive error message given that the `Cat` type itself is a subtype of `Animal`.

I propose error messages which show a suggestion to change `class Cont` to `class Cont<+T>`. So that:
- if `A <: B` but `C `, then an error message like "`A` is a subtype of `B`, but `C` is not covariant in type argument `T`. Did you mean to define `C<+T>` instead of `C`? " should be shown.
- if `B <: A` but `C
`, then an error message like "`B` is a subtype of `A`, but `C` is not contravariant in type argument `T`. Did you mean to define `C<-T>` instead of `C`?" should be shown.

(`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.