【发布时间】:2017-01-23 07:27:58
【问题描述】:
我举个例子
/* @flow */
class Foo {}
class Bar {}
declare var f: ((x: Foo) => void) & ((x: Bar) => void);
f(new Foo());
来自文档页面https://flowtype.org/docs/union-intersection-types.html#_
并且这种代码类型检查没有错误。
对我来说,结果并不是很明显。
当它们显示在页面顶部附近的某个位置时,还有另一个示例:
/* @flow */
type I = {a: number} & {b: number};
var x: I = {a: 1, b: 2};
x = {a: 1, b: 2, c: "three"};
交集(源自术语语义本身)是 2 种(或更多)类型的复合体。基本上是AND。
那么,为什么f(new Foo()); 没有通过类型检查呢? new Foo() 参数显然不是 Bar 的实例,因此不能通过。
我错过了什么?
UPD
经过更多研究,我发现| 和& 的含义在您使用declare var 时互换(相对于type 或就地输入)。不过,我找不到解释为什么它甚至首先发生。
【问题讨论】:
标签: javascript flowtype