[英]ESLint - missing return type on function react component - Typescript error explanation
I already saw some of the answers for a similar question when the name of the component is defined, but I would like to understand more the logic behind ESLint with components exported as defaults as in my case.在定义组件名称时,我已经看到了类似问题的一些答案,但我想更多地了解 ESLint 背后的逻辑,其中组件导出为默认值,就像我的情况一样。
({ children }: AppProviderProps) => - according to ESLint: "Missing return type on function.eslint@typescript-eslint/explicit-module-boundary-types". ({ children }: AppProviderProps) => - 根据 ESLint:“function.eslint@typescript-eslint/explicit-module-boundary-types 上缺少返回类型”。 Could someone explain to me why as I always thought it's a standard way to write React components?有人可以向我解释为什么我一直认为这是编写 React 组件的标准方式吗? And what would be the solution in that case?在这种情况下,解决方案是什么?
import React, { ReactNode } from 'react';
import { Provider } from 'react-redux';
import store from '../../@state/createStore';
export default ({ children }: AppProviderProps) => (
<Provider store={store}>
**my code**
</Provider>
);
interface AppProviderProps {
children: ReactNode;
}
PS I know I can disable the rule, but would like to know how to write so ESLint wouldnt scream at me. PS 我知道我可以禁用该规则,但想知道如何编写以便 ESLint 不会对我尖叫。
The rule typescript-eslint/explicit-module-boundary-types is telling you that your function requires and explicit return type.规则typescript-eslint/explicit-module-boundary-types告诉您 function 需要和显式返回类型。 From the source:从来源:
Explicit types for function return values and arguments makes it clear to any calling code what is the module boundary's input and output. function 返回值和 arguments 的显式类型使任何调用代码都清楚什么是模块边界的输入和 output。
So you need to add the appropriate return type, which in your example is whatever the type of <Provider />
is:因此,您需要添加适当的返回类型,在您的示例中是<Provider />
的类型是什么:
Replace TypeOfProviderHere
with the correct type.将TypeOfProviderHere
替换为正确的类型。
export default ({ children }: AppProviderProps): TypeOfProviderHere => (
<Provider store={store}>
**my code**
</Provider>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.