facebook / facebook/flow

Flow does not accept intersection function types even with disjoint unions

未关闭
#3,021 12 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看
feature request Typing: unions/intersections
主要语言
Rust
星标
22.3k
派生
1.9k
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。