简体   繁体   English

流程:使用地图时出现错误

[英]Flow: Getting error when using Map

I have just started to implement type checking using Facebook Flow in one of my projects, and have encountered some issues. 我刚刚开始在我的一个项目中使用Facebook Flow实现类型检查,并且遇到了一些问题。 I am trying to do the following with a Map: 我正在尝试对地图执行以下操作:

/* @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);

But I am getting an error saying: 但我收到一个错误消息:

Cannot call `testMap.get(...).push` because property `push` is missing in undefined [1]

Se example in try flow: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVMCmAXM3MDO2AsgIYAOAXGGeQDxEBOAlgHYDmANGAIKOOkAnnVYBXALYAjTIwB8ssAF4wrTEloAKAJQBuDDjABrTIOpM27JWABE+IgGVsLDtb2pmUMBoCEdkhQA6AAtSAg1jQS0tMABvVDA8Qn9yAIIccJNuAG0AXS1UAF90P1oA9nSIrQDyUQIgjQBGXVQgA 硒例如,在尝试流程: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVMCmAXM3MDO2AsgIYAOAXGGeQDxEBOAlgHYDmANGAIKOOkAnnVYBXALYAjTIwB8ssAF4wrTEloAKAJQBuDDjABrTIOpM27JWABE+IgGVsLDtb2pmUMBoCEdkhQA6AAtSAg1jQS0tMABvVDA8Qn9yAIIccJNuAG0AXS1UAF90P1oA9nSIrQDyUQIgjQBGXVQgA

This is of course because the get function in the Map interface is defined as: 这当然是因为Map接口中的get函数定义为:

get(key: K): V | void;

But I was expecting Flow to recognize that the key is actually being set just above. 但是我期望Flow能够意识到该键实际上是在上方设置的。

Any suggestions on how I should change my code in order to make Flow happy? 关于如何更改代码以使Flow满意的任何建议?

Thank you very much! 非常感谢你!

As you mentioned, the problem here is that your call to Map.get may return void as you can see in V | void 如您所提到的,这里的问题是您对Map.get的调用可能返回void就像在V | void看到的那样V | void V | void . V | void

Flow has no chance to know whether your key was defined or not as this may change during runtime. Flow没有机会知道您的密钥是否已定义,因为在运行时可能会更改。

Thus you need to check whether the returned value is not undefined before accessing it's push method. 因此,您需要在访问返回值的push方法之前检查返回的值是否undefined

const arr = testMap.get(key);

if (arr) {
  arr.push(1)
} else {
  // map "testMap" didn't contain a value for key "key"
}

full demo in flow repl: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVMCmAXM3MDO2AsgIYAOAXGGeQDxEBOAlgHYDmANGAIKOOkAnnVYBXALYAjTIwB8ssAF4wrTEloAKAJQBuDDjABrTIOpM27JWABE+IgGVsLDtb2pmUMBoCEdkhQA6AAtSAg1jQS0tMABvVDA8Qn9yAIIccJNuAG0AXS1UAF90AGM4ViIwUn4rP1oA9nSI3XQPLyrGaLiE9oDyUQIgjQBGfIKwTBg02PiwYGAwcQobWoprMAATZnXWAHJcUtZsUjZKsAA3UhhRTDAoOEYjExsI60KgA 在流动REPL充分演示: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVMCmAXM3MDO2AsgIYAOAXGGeQDxEBOAlgHYDmANGAIKOOkAnnVYBXALYAjTIwB8ssAF4wrTEloAKAJQBuDDjABrTIOpM27JWABE+IgGVsLDtb2pmUMBoCEdkhQA6AAtSAg1jQS0tMABvVDA8Qn9yAIIccJNuAG0AXS1UAF90AGM4ViIwUn4rP1oA9nSI3XQPLyrGaLiE9oDyUQIgjQBGfIKwTBg02PiwYGAwcQobWoprMAATZnXWAHJcUtZsUjZKsAA3UhhRTDAoOEYjExsI60KgA


Another approach would be like this: 另一种方法是这样的:

let arr = testMap.get(key);

if (!arr) {
  arr = [];
  testMap.set(key, arr);
}

arr.push(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM