在流联合中类型精炼,流不能识别if语句

问题描述 投票:0回答:1

考虑以下简单代码:

/* @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 callfoowithvbound tobbecause object type [1] is incompatible with number [2] in propertyvalue.

javascript casting flowtype
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.