如何设置 Flow 类型声明 - lint 出现错误 "not defined"?

How to set up Flow type declarations - lint up with error "not defined"?

我刚开始使用类型化 JS,目前 exporting/importing 我的类型。在查了一些东西之后,我认为正确的解决方案似乎是 "declarations" 的想法。

阅读[https://flowtype.org/docs/declarations.html#pointing-your-project-to-declarations]后,我尝试了“.flowconfig”风格。

decls/types.js

declare type Post = {
  feed: Connection,
}

declare type Connection = {
  edges: Array<Edge>,
  pageInfo: PageInfo,
}

declare type Edge = {
  cursor: number,
  node: Node,
}

declare type PageInfo = {
  endCursor: number,
  hasNextPage: boolean,
}

declare type Node = {
  id: string,
  createdAt: number,
}

然后我将 decls/ 目录添加到我的 .flowconfig [libs].

.flowconfig

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js
node_modules/react-native/flow
flow/
decls/

然而,所有类型都与未定义的错误一起出现,例如'Connection' 未定义。

有什么我遗漏的吗?

由于您使用的是 ESLint,因此您需要添加 eslint-plugin-flowtype extension and minimally enable the define-flow-type 规则,以便 ESLint 不会将流类型标记为未定义。

ESLint 配置:

{
  "plugins": [
    "flowtype"
  ],
  "rules": {
    "flowtype/define-flow-type": 2
  }
}