是否可以在 React 中使用 Flow 和箭头函数?

Is it possible to use Flow and arrow functions with React?

正在逐步迁移项目以使用 Flow 类型。但是,我们的任何箭头函数都会出现以下错误:

无法为该模块构建类型化接口。您应该使用类型注释此模块的导出。 属性 handleChange:Flow(signature-verification-failure)

处缺少类型注释

使用以下语法,Flow 不会报错:

  constructor(props: ComponentProps) {
      super(props);
      this.state = {
         query: undefined,
      }
      (this: any).handleChange = this.handleChange.bind(this);
  }

  handleChange(event: any) {
    this.setState({ query: event.target.value });
  }

但是,为了避免对我们拥有的每个辅助函数进行绑定——在某些组件中有很多辅助函数——(以及来自之前的迁移),我们目前使用如下箭头函数:

  handleChange = (event: any) => {
    this.setState({ query: event.target.value });
  }

通过此更改,我们得到了上述错误。不确定为什么会发生;是否有任何解决方法或方法可以让流程在没有 FlowIgnores 的情况下停止抱怨?谢谢!

我相信你想要这种风格:

class Klass {
  member: (string => number) = (num) => num.length;
}

(try)