【发布时间】:2023-03-25 12:25:01
【问题描述】:
我是我的 Typescript 项目中的一个有状态的 React 组件。我使用 ESLint 使用 @typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 对其进行 lint。我已启用规则@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 和 render 方法出现 ESLint 错误:
Missing return type on function - @typescript-eslint/explicit-function-return-type
总的来说,我非常喜欢 lint 规则,但我当然不必为所有这些 React 方法声明返回类型。我已经安装了@types/react 和@types/react-dom,那么这些返回类型不是已经涵盖了吗?
【问题讨论】:
-
您是否检查了允许继承类型选项的设置?
-
@AvinKavish 我还没有检查过,但现在会检查。我的 ESLint 扩展是:
"plugin:react/recommended", "plugin:@typescript-eslint/recommended"所以虽然这将是默认值?! -
哦,我的意思是在您安装的插件中,没关系我刚刚检查过,它没有这样的选项。您可能需要考虑在 GitHub 上打开一个问题并直接询问作者。
-
@AvinKavish 好的,感谢您的帮助
标签: javascript reactjs typescript