假设有一个外部模块导出了一个 React 组件。该模块没有流声明。我想声明它以启用流类型检查。
例如,该模块的定义如下:
import React from 'react'
class External extends React.Component {
static propTypes = {
name: React.PropTypes.string.isRequired
}
render() {
return <div></div>
}
}
如果我写的是我自己(内部模块)Flowtype 将能够检测到缺失
Prop 。但是,由于 External
组件是从外部模块导出的。
Flow 不进行类型检查。所以,我需要创建单独的类型声明
对于该模块。
我尝试过这个:
// ./interfaces/the-external-module.js.flow
declare module "the-external-module" {
declare export var External: React.Component;
}
当然它不起作用,因为React不在范围内。那么,我怎样才能
在单独的类型声明文件中将 External
声明为 React.Component
?
最佳答案
根据一些official definitions ,使用 React$Component
(在源 here 中定义);例如
declare module "the-external-module" {
declare export var External: React$Component;
}
关于javascript - 如何使用单独的声明文件从外部模块声明 React.Component 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38932747/