reactjs - 隐式获取任何绑定元素子项的类型错误
问题描述
我正在将 jsx 代码转换为打字稿,其中我遇到了如下错误
绑定元素 'children' 隐式具有任何类型
我在 jsx 中的代码如下
const GoogleMap = ({ children, ...props }) => (
<div style={{ height: '80vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{
key: environment.googleApiKey,
}}
{...props}
>
{children}
</GoogleMapReact>
</div>
);
GoogleMap.propTypes = {
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
};
GoogleMap.defaultProps = {
children: null,
};
export default GoogleMap;
上面的代码现在转换为打字稿如下
const GoogleMap = ({ children, ...props }) => (
<div style={{ height: '80vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{
key: environment.googleApiKey,
}}
{...props}
>
{children}
</GoogleMapReact>
</div>
);
export default GoogleMap;
我已经尝试将孩子设置为任何但它不起作用。因为我是打字稿的新手,所以我无法弄清楚
解决方案
您应该像这样为组件分配一个接口
interface GoogleMapProps {
exampleprop: string
exampleprop: number
}
const GoogleMap: React.FC<GoogleMapProps> = ({ children, ...props }) => (
<div style={{ height: '80vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{
key: environment.googleApiKey,
}}
{...props}
>
{children}
</GoogleMapReact>
</div>
);
export default GoogleMap;
React.FC
代表 React.FunctionalComponent 。这包括 children 属性,它返回一个返回 JSX 的函数。传递给的接口React.FC
是所有其他道具
推荐阅读
- angular - 拆分具有太多属性的类 - 角度
- python - 运行 CMAKE 找不到 python 可执行文件
- swiftui - 在没有导航链接的情况下更改视图 SwiftUI
- python - 如何从 Mac 上的另一个可执行文件/Python 脚本调用可执行文件?
- python-3.x - Heroku Python没有名为Mysql的模块
- android-studio - 如何在 android studio 中为 artifactory 创建 pom 文件?
- javascript - 我怎样才能等待 2 个承诺完成,然后再运行另一个?
- django - django中的slug错误-模板显示每个帖子中的所有帖子数据
- kubernetes - 使用外部 Prometheus 抓取 CoreDNS 指标
- c++ - 索引访问器时的 EXC_BAD_ACCESS (code=EXC_I386_GPFLT)