javascript - 每个 React 类方法的“函数缺少返回类型”
问题描述
我的 Typescript 项目中有一个有状态的 React 组件。我使用 ESLint 使用@typescript-eslint/parser
and对它进行 lint @typescript-eslint/eslint-plugin
。我已经启用了规则@typescript-eslint/explicit-function-return-type
。
我的组件看起来与此类似:
interface Props = {
name: string;
}
interface State = {
score: number
}
class Person extends Component<Props, State> {
state = {
score: 2,
};
componentDidMount() {
...
}
render() {
...
}
}
在上面的组件中,我在componentDidMount
andrender
方法上得到 ESLint 错误:
Missing return type on function - @typescript-eslint/explicit-function-return-type
总的来说,我非常喜欢 lint 规则,但我当然不必为所有这些 React 方法声明返回类型。我已经安装@types/react
并@types/react-dom
安装了,所以这些返回类型不是已经涵盖了吗?
解决方案
尝试将渲染函数的返回类型编写为
render(): JSX.Element {
// render code
}
这对我有用!
推荐阅读
- javascript - 将图像的 url 分配给标记html
- javascript - 单项数组上的 Array.sort() 是否在不触发回调函数的情况下返回?
- python - 保留数据框中的行,对于某些列的值的所有组合,在另一列中包含相同的元素
- postgresql - Postgresql 错误:小时“16”对于 12 小时制无效
- kubernetes - 如何在具有无盘工作节点的 kubernetes 集群中安全地 NFS 挂载 /var/lib/kubelet?
- postgresql - PostgreSQL Warm Standby 性能真的很慢
- r - 是否可以在 Rstudio 中使用 SpatStat 向 3D 点图案添加标记?
- android - 为什么我只能从模拟器而不是在我的真实设备上选择列表视图的项目?
- javascript - 如何在具有相同类名的 ul 列表中附加 li 元素?
- python - AWS Python Lambda 函数 - 将 UTF-8 编码的 CSV 写入 S3