简体   繁体   English

ESLint - function 反应组件上缺少返回类型 - Typescript 错误解释

[英]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.

相关问题 反应:缺少函数的返回类型。 eslint(@typescript-eslint/explicit-function-return-type) - React: Missing return type on function. eslint(@typescript-eslint/explicit-function-return-type) 打字稿 | 关于缺少函数返回类型的警告,ESLint - Typescript | Warning about Missing Return Type of function, ESLint 缺少函数的返回类型 - 在反应(打字稿)代码中 - Missing return type on function - in react (typescript) code FlatList Typescript 错误缺少 function 上的返回类型 - FlatList Typescript error missing return type on function Typescript React/Nextjs styled-components - 将函数作为道具传递给组件会在缺少类型时引发错误 - Typescript React/Nextjs styled-components - passing function as prop to component throws error on missing type Typescript React/styled-components - 将 function 作为组件传递给组件会在缺少类型时引发错误 - Typescript React/ styled-components - passing function as prop to component throws error on missing type 显式函数返回类型创建功能组件在打字稿中做出反应 - explicit-function-return-type creating functional component react in typescript 打字稿反应组件中的反应/道具类型eslint错误 - react/prop-types eslint error in typescript react component 异步 function 作为 prop 传递给 React 组件,导致 @typescript-eslint/no-misused-promises 错误 - Async function passed as prop into React component causing @typescript-eslint/no-misused-promises error 样式组件插值函数中的“缺少函数返回类型”错误 - "Missing return type on function" error in styled component interpolation function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM