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

ffx*_*sam 1 javascript flowtype

我有以下类型别名:

type TestType = {
  critical: string,
  failProps: Object,
  successProps: ?Object,
  test: Function,
};
Run Code Online (Sandbox Code Playgroud)

我想更具体一点test.该功能应该是这个签名:

function (value: string): boolean { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

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

小智 5

您可以使用以下语法:

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

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

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