【发布时间】:2018-05-06 08:28:53
【问题描述】:
我刚刚开始在我的一个项目中使用 Facebook Flow 实现类型检查,并且遇到了一些问题。我正在尝试使用地图执行以下操作:
/* @flow */
let testMap: Map<string, Array<number>> = new Map();
let key: string = "testString";
if (!testMap.has(key)) {
testMap.set(key, [])
}
testMap.get(key).push(1);
但我收到一条错误消息:
Cannot call `testMap.get(...).push` because property `push` is missing in undefined [1]
这当然是因为Map接口中的get函数定义为:
get(key: K): V | void;
但我希望 Flow 能够识别出密钥实际上是在上面设置的。
关于我应该如何更改代码以使 Flow 快乐的任何建议?
非常感谢!
【问题讨论】:
标签: javascript flowtype static-typing