【发布时间】: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.jsflow-typed文件中,如问题的屏幕截图所示。
标签: reactjs react-router flowtype react-router-v4 flow-typed