【问题标题】:Unable to figure out why certain part of the function is not covered, flow无法弄清楚为什么功能的某些部分没有被覆盖,流程
【发布时间】:2018-03-03 07:11:53
【问题描述】:

我有这个功能可以展平一个物体

export function flattenObject(object: Object, prefix: string = "") {
  return Object.keys(object).reduce((messages, key) => {
    const value: Object | string = object[key];
    const prefixed = prefix ? `${prefix}.${key}` : key;
    const flatMessages = { ...messages };
    if (typeof value === "string") {
      flatMessages[prefixed] = value;
    } else {
      Object.assign(flatMessages, flattenObject(value, prefixed));
    }
    return flatMessages;
  }, {});
}

在第 3 行,这部分 object[key] 说它未覆盖

[流覆盖] 未发现代码(参数)对象:Object [Flow] 对象:对象

我不完全确定为什么,因为它确实说它是一个对象?然而,物体的形状可能会有所不同,所以我最初的假设可能是因为它的定义松散?如果是这样,警告消息是否有解决方法?

【问题讨论】:

    标签: javascript flowtype flow-typed


    【解决方案1】:

    我相信这是由于 Flow 期望您的对象参数有比 Object 更好的注释,您可以尝试 {} 以获得快速胜利,或者按照 here 的描述为其创建类型注释(推荐)。

    例如

    flattenObject(object: { foo: string }, prefix: string = "") { ...
    

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 2019-05-05
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多