【问题标题】:Return type checking in FlowFlow 中的返回类型检查
【发布时间】:2019-04-09 12:08:39
【问题描述】:

Flow 是否应该检查返回类型?它似乎不适用于自定义类型 - 以下代码编译得很好:

function test1(value: number) : Distance {
  if (value == 4) {
    return "asd";
  }
  if (value == 5) {
    return { a: 9 };
  }
  if (value == 6) {
    return null;
  }

  return new Distance(value);
};

距离定义如下:

export class Distance {
  value: string;
  unit: string;

  constructor(value: string, unit?: SDKConstants.MeasurementUnit) {
    this.value = value;
    this.unit = unit;
  }
}

这在 Flow 上编译得很好。我注意到如果我将返回类型更改为数字,则会出现一些错误。

有人能解释一下 Flow 在这种情况下的局限性吗?我需要在配置中进行设置以使其更严格还是什么?

【问题讨论】:

    标签: javascript react-native flowtype


    【解决方案1】:

    问题是距离是在一个没有@flow 标签的单独文件中定义的。另一种方法是在所有文件中启用流检查 (https://flow.org/en/docs/config/options/#toc-all-boolean)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-13
      • 2016-08-03
      • 2016-12-08
      • 1970-01-01
      • 2020-12-11
      • 2020-02-06
      • 2020-01-08
      • 2020-11-25
      相关资源
      最近更新 更多