Flow 中箭头函数接口中的签名验证失败

signature-verification-failure in Interface on Arrow function in Flow

我最近将一个使用 Flow 的项目更新到了 0.138.0 版。 更新后,这是我第一次遇到类似这样的错误:

Cannot build a typed interface for this module. You should annotate the exports of this module with types. Missing type annotation at property _dispatchUpdate: [signature-verification-failure]

我们的项目由许多 类 组成,其中一些看起来像这样

export class SomeActions {
  _interval: IntervalID;
  _isPolling: boolean;

  _dispatchUpdate = (downloadItems: DownloadItems[]): void => {
     AppDispatcher.dispatch({
       actionType: CONSTANT.CONSTANT,
       downloadItems,
     })
  }
} 

export default new SomeActions();

为了解决问题中的错误,我开始实现如下接口

interface SomeActionsT {
  _interval: IntervalID;
  _ isPolling: boolean;
  
  _dispatchUpdate: (DownloadItems[]): void;
}

export class SomeActions implements SomeActionsT {
  _interval: IntervalID;
  _isPolling: boolean;

  _dispatchUpdate = (downloadItems: DownloadItems[]): void => {
     AppDispatcher.dispatch({
       actionType: CONSTANT.CONSTANT,
       downloadItems,
     })
  }
} 

export default new SomeActions();

但是流量错误依然存在。如果我将 _dispatchUpdate 重构为一个普通函数,它就不会再抱怨了。但那很愚蠢。我怎样才能让流程识别箭头功能?

首先,您收到这些错误可能是因为您从默认情况下没有类型优先的旧版本进行更新。已解释

要解决您的问题,您可以像这样输入 class

export class SomeActions {
  _interval: IntervalID;

  _isPolling: boolean;

  _dispatchUpdate: DownloadItems[] => void = (downloadItems) => {
    AppDispatcher.dispatch({
      actionType: CONSTANT.CONSTANT,
      downloadItems,
    });
  }
}

export default (new SomeActions(): SomeActions);

因为要导出 new Class,所以还必须明确声明其类型,否则 export default SomeActions; 就足够了。