流联合中的类型细化,流无法识别if语句

时间:2019-03-23 02:35:28

标签: javascript casting flowtype

请考虑以下简单代码:

/* @flow */

type A = {|
  value: {id: number} | number,
  value2: string,
|}


type B = {
  ...$Exact<A>,
  value: number,
}


function foo(b:B, add: number) {
  console.log(b.value + add);
  console.log(b.value2)
}

const v: A = {
  value: 2,
  value2: 'a',
}
if (typeof v.value === 'number') {
  foo(v);
}

typeof应该指示类型已精炼为“数字”,因此它可以是foo的参数。但是,当我测试此in a fiddle时,显示了错误。 (Cannot call foo with v bound to b because object type [1] is incompatible with number [2] in property.

1 个答案:

答案 0 :(得分:0)

将建议阅读以下问题:https://github.com/facebook/flow/issues/4685