Flow does not accept intersection function types even with disjoint unions
- Lenguaje dominante
- Rust
- Estrellas
- 22.3k
- Forks
- 1.9k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
Hi!
I have the following code ([try link](https://flowtype.org/try/#0C4TwDgpgBAglC8UDeAoKVSQFxQOQENcAaNKfHAZ2ACcBLAOwHMSBfAbhRU2gCEFlS3HLgBGxUiJz0ArgFsREaqw5dw0AGL8AFFvKwAlAgB8UKnSaGAZFB2SoPQ-BMz5i-RwDGAe3pUoAMy8vHE1EXTs4AB97RxNUdFp-G3wRADpuBHhEAlxDePQoaghgaWp6MjT8DnQWKAgAGwpofPQikrKK1JFqqBYUFiA))
```jsx
type A = {
type: 'a',
a: string,
};
type B = {
type: 'b',
b: number,
};
type F = ((a: A) => string) & ((b: B) => number);
const foo: F = (ab: A | B) => {
if (ab.type === 'a') {
return ab.a;
} else {
return ab.b;
}
}
```
I wish to make a function `foo` accepting a disjoint union but which returns different types for each member.
This fails with the following error:
```
14: return ab.a;
^ string. This type is incompatible with
11: type F = ((a: A) => string) & ((b: B) => number);
^ number
16: return ab.b;
^ number. This type is incompatible with
11: type F = ((a: A) => string) & ((b: B) => number);
^ string
```
What's the best way to express this type of function (disjoint types input, but different output type for each one)? I would like to not have to declare it as `(ab: A|B) => string|number` because that loses some information.
Sincerely,
Dan
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.