简体   繁体   English

根据变量的类型调用不同的函数时,正确的流类型模式是什么?

[英]What's the correct Flow type pattern when calling different functions depending on the type of a variable?

I'm kind of new to Flow and changing from "do whatever you like with the code" style in javascript to the more strict typing Flow forces you is a pain sometimes.我对 Flow 有点陌生,从 javascript 中的“随心所欲地使用代码”样式更改为更严格的类型 Flow 有时会迫使您感到痛苦。

I have a class with a function returning the following:我有一个 class 和一个 function 返回以下内容:

const { id: idParam, params } = this.config[tag];
return params.map((param: string | ObjectConfigParam) => {
  const paramType = typeof param;
  let ret = { id: element.id, tag, position };
  if (paramType === 'string') {
    ret = { ...ret, ...this.resolveStringParam(element, param) };
  } else if (paramType === 'object') {
    ret = { ...ret, ...this.resolveObjectParam(element, param) };
  }
  return ret;
})

As params can be two different things (a simple string or a more complex ObjectConfigParam ) I've defined to different functions to process them.由于params可以是两个不同的东西(一个简单的string或更复杂的ObjectConfigParam ),我已经定义了不同的函数来处理它们。 The signature of the functions are:函数的签名是:

resolveStringParam(element: any, param: string): Field

resolveObjectParam(element: any, param: ObjectConfigParam): Field

Running the flow checker reports the following error: Cannot call this.resolveStringParam with param bound to param because ObjectConfigParam [1] is incompatible with string [2].运行流检查器报告以下错误: Cannot call this.resolveStringParam with param bound to param because ObjectConfigParam [1] is incompatible with string [2]. for the first call and Cannot call this.resolveObjectParam with param bound to param because string [1] is incompatible with ObjectConfigParam [2].对于第一次调用,并且Cannot call this.resolveObjectParam with param bound to param because string [1] is incompatible with ObjectConfigParam [2]. for the second one.对于第二个。

You need to place typeof param inside the if condition.您需要将typeof param放在if条件中。 By the way, you don't need the second if .顺便说一句,你不需要第二个if

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDyARgFZ4DGGAwnAHZQCWA5gAoCGATuwLZgC8YAN5gAznB54AauxgBXPAC4xGTozrMwAXwDc6VFDl0qjemE55xMAG54AyqvVsuvABSjVyj2o0BKYVoGRiZmFla2JORUHNw8rnBkypGUNPRMzrH+QoGoFPQeYAAeAmCuOC48Xo4aYAA+RGQptAwsMbz+-AB8wqhgYIxQpdj4cIPlsQL8ggDk3k7TWb195pZwNvbVGW7j7Xp9WmB4MKIEQkt9YWsRjdEVZRW+e9qoWkA https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDyARgFZ4DGGAwnAHZQCWA5gAoCGATuwLZgC8YAN5gAznB54AauxgBXPAC4xGTozrMwAXwDc6VFDl0qjemE55xMAG54AyqvVsuvABSjVyj2o0BKYVoGRiZmFla2JORUHNw8rnBkypGUNPRMzrH+QoGoFPQeYAAeAmCuOC48Xo4aYAA+RGQptAwsMbz+-AB8wqhgYIxQpdj4cIPlsQL8ggDk3k7TWb195pZwNvbVGW7j7Xp9WmB4MKIEQkt9YWsRjdEVZRW+e9qoWkA

type ObjectConfigParam = { someValue: string };

function resolveStringParam(str: string) {}
function resolveObjectParam(obj: ObjectConfigParam) {}

const x = (param: string | ObjectConfigParam) => {
  if (typeof param === 'string') {
    resolveStringParam(param);
  } else {
    resolveObjectParam(param);
  }
}

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

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