【问题标题】:Why do flow-typed libdefs types not work consistently?为什么流类型的 libdefs 类型不能始终如一地工作?
【发布时间】:2017-12-06 23:27:06
【问题描述】:

我有一个使用 Flow 进行静态类型检查的 React 项目;我有使用flow-typed 安装的 npm 3rd 方库的 libdefs。但是,有时无法识别类型。例如,这里是一个使用 react-router-dom npm 包的 React 组件,当使用来自相应 flow-typed/npm/react-router-dom_v4.x.x.js 的类型时出现 identifier ... Could not resolve name Flow 错误:

类型在 libdef 中:

我正在对其他 flow-typed 类型执行此操作,并且它正在工作。我已经确认我在package.json 中安装了最新的库,并在flow-typed libdef 中安装了最新的相关库:

来自https://github.com/flowtype/flow-typed/tree/master/definitions/npm/react-router-dom_v4.x.x

我也尝试过像这样的 ES6 模块一样显式导入类型:

import type { RouterHistory } from 'react-router-dom'`

但这会导致RouterHistory not found in 'react-router-dom' 错误,即使您可以在上面的屏幕截图中看到它存在于来自flow-typed/npm/react-router-dom_v4.x.x.js libdef 中;我相信只有在 npm 库中有 *.js.flow 文件(react-router-dom 没有但在 flow-typed 中定义)时,类型的显式导入才有效。

所以问题是:如果RouterHistory 类型在相关的flow-typed libdef 中定义,为什么它不起作用

【问题讨论】:

  • 您绝对需要问题末尾的导入行。一旦你这样做了,你会得到什么错误?
  • 为什么需要导入行?我在flow-typed 中使用的所有其他libdef 都可以在没有类型导入的情况下工作。就像我可以使用 React 中的 SyntheticEvent 一样。即便如此,添加import type { RouterHistory } from 'react-router-dom' 会导致RouterHistory not found in 'react-router-dom' 错误,即使RouterHistory 类型在react-router-dom_v4.x.x.js flow-typed 文件中,如问题的屏幕截图所示。

标签: reactjs react-router flowtype react-router-v4 flow-typed


【解决方案1】:

您的导入语句是错误的。应该是这样的:

import type { RouterHistory } from 'react-router-dom'

编辑:

RouterHistory 类型似乎在 react-router 包中。由于 react-router-dom 使用 react-router 作为基本依赖项,因此您应该添加带有流类型的 react-router 包。 Flow-React-Router

【讨论】:

  • 这会导致另一个错误:RouterHistory not found in 'react-router-dom'react-router-dom 中没有 *.js.flow 文件,所以这不起作用?
  • 啊,好吧,RouterHistory 类型似乎不是 react-router-dom 包的一部分。相反,您应该通过流类型安装 react-router 包。我编辑了我的答案。
  • 如截图所示,RouterHistoryreact-router-dom_v4.x.x.jsflow-typed文件i.stack.imgur.com/w7mMr.png
猜你喜欢
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2019-06-24
  • 2020-03-11
相关资源
最近更新 更多