如何注释对象中属性的函数?

时间:2016-07-01 02:20:53

标签: javascript flowtype

我有以下类型别名:

type TestType = {
  critical: string,
  failProps: Object,
  successProps: ?Object,
  test: Function,
};

我想更加具体test。该函数应具有此签名:

function (value: string): boolean { /* ... */ }

如何指示test应采用单个字符串参数并返回布尔值?

1 个答案:

答案 0 :(得分:5)

您可以使用以下语法:

type TestType = {
  critical: string,
  failProps: Object,
  successProps: ?Object,
  test: (value: string) => boolean,
};

...或者如果你想在其他地方重用函数类型:

type functionType = (value: string) => boolean
type TestType = {
  critical: string,
  failProps: Object,
  successProps: ?Object,
  test: functionType,
};