facebook / facebook/flow

Flow does not accept intersection function types even with disjoint unions

Abierto
#3,021 12 comentarios 3 reacciones 0 asignados Ver en GitHub
feature request Typing: unions/intersections
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

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.