打字稿|关于函数ESLint的缺少返回类型的警告 [英] Typescript | Warning about Missing Return Type of function, ESLint

查看:751
本文介绍了打字稿|关于函数ESLint的缺少返回类型的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有TypeScript的项目中,我有一个 REACT-STATELESS-COMPONENT

I have a REACT-STATELESS-COMPONENT, in a project with TypeScript. It has an error, saying, that

Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)

我不确定它要我做什么。这是我的代码:

I am not sure what it wants me to do. Here is my code:

import React, { Fragment} from 'react';
import IProp from 'dto/IProp';

export interface Props {
  prop?: IProp;
}

const Component = <T extends object>({ prop }: Props & T) => (
  <Fragment>
    {prop? (
      <Fragment>
        Some Component content..
      </Fragment>
    ) : null}
  </Fragment>
);

LicenseInfo.defaultProps: {};

export default Component;

你能告诉我我需要做什么。我需要阅读有关TS的信息,但目前我根本不了解它。

Can you tell me what I need to do. I need to read about TS, but currently I don't get it at all. And I can't commit right now cause of this.

推荐答案

我建议使用react提供的类型。它们将包含返回类型。如果您使用的是版本16.8.0或更高版本的react,请执行以下操作:

I'd recommend using the types provided by react; they'll include the return type. If you're on the version 16.8.0 or later of react, do this:

const Component: React.FunctionComponent<Props> = (props) => (

或使用速记:

const Component: React.FC<Props> = (props) => (

在16.8之前,您应该这样做:

Prior to 16.8, you'd instead do:

const Component: React.SFC<Props> = (props) => (

SFC代表无状态功能组件。他们更改了名称,因为功能组件不再必须是无状态的。

Where SFC stands for "stateless functional component". They changed the name, since function components are no longer necessarily stateless.

这篇关于打字稿|关于函数ESLint的缺少返回类型的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆