javascript - 无法在 flow-js 中将类型设置为 Array<string>

标签 javascript reactjs visual-studio-code flowtype

函数 getRowData 的最后一行之前的一个流程中出现以下错误:

message: 'Cannot call getRow with el.arr bound to data because: Either property arr of unknown type [1] is incompatible with array type [2]. Or property arr of unknown type [1] is incompatible with string [3]. (References: [1] [2] [3])' at: '60,44' source: 'flow' code: 'undefined'

仅当我更改 Array<string> 时,此错误才会消失在type UserPropsArray<mixed> ,但我想保留字符串数组。

我不明白为什么会出现此错误。我明确设置data.audiodata.chatArray<string> 。任何帮助将不胜感激!

type UserProps = {
  name: string,
  audio: Array<string>,
  chat: Array<string>,
  enableChat: boolean,
  enableAudio: boolean,
  status: string,
};

const getRow = (key, data: string[] | string, style = {}) => {
  const text = Array.isArray(data) ? data.join(", ") : data;
  return (
    <TableRowColumn
      key={`row-${key}`}
      style={{ whiteSpace: "pre-wrap", ...style }}
    >
      {text}
    </TableRowColumn>
  );
};

const getRowData = (data: UserProps) =>
  [
    data.name,
    data.status,
    {
      arr: data.audio,
      style: { color: data.enableAudio ? "green" : "red" }
    },
    { arr: data.chat, style: { color: data.enableChat ? "green" : "red" } }
  ].map(
    (el, i) =>
      el && el.arr && el.style ? getRow(i, el.arr, el.style) : getRow(i, el) // error in el.arr !!!!!
  );

最佳答案

错误有点令人困惑,但问题是 flow 无法细化 el 类型。您可以做的是稍微改变细化:

typeof el === 'string' ? getRow(i, el) : getRow(i, el.arr, el.style)

你可以试试here

关于javascript - 无法在 flow-js 中将类型设置为 Array<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49531337/

相关文章:

javascript - 如果它与 javascript 垂直,则尝试将动态图像居中

javascript - 使用 React Native 打开键盘时更新 ScrollView

reactjs - 为什么 react-router 在调度时自动返回到以前的路由

python-3.x - 如何使用 Visual Studio Code 在 vi​​rtualenv 中执行 python 命令

python - 如何通过命令行在 VS Code 中访问更新的 Python 3.9?

javascript - 在 THREE.js 中检测该对象内的单击对象

javascript - 在 Angular JS 中缓存结果

javascript - 如何在向下滚动时转换我的 Img 大小

javascript - 模拟获取 : Jest test says response is wrong, 但实际运行代码按预期工作

visual-studio-code - 如何使可视代码终端和编辑器看起来像pycharm