繁体   English   中英

反应:缺少函数的返回类型。 eslint(@typescript-eslint/explicit-function-return-type)

[英]React: Missing return type on function. eslint(@typescript-eslint/explicit-function-return-type)

我是 Typescript React 的新手。 在一个非常基本的功能组件中,eslint 抱怨我它缺少功能组件本身的返回类型。 我错在哪里?

在此处输入图片说明

正如 Nicholas Tower 在这个答案中所解释的那样Typescript | 关于缺少函数返回类型 ESLint 的警告,根据您使用的React版本,您可以使用以下任何行:

如果您使用的是最新的 React 版本(16.8.0 或更高版本),请执行以下操作: const Component: React.FunctionComponent<Props> = (props: Props) => { }

在 16.8 之前,您应该这样做: const Component: React.SFC<Props> = (props: Props) => {}

SFC 代表“无状态功能组件”。

编辑: - - -

根据@SergioP 的评论,更惯用的解决方案是

const Test = ({ title }: Props): JSX.Element => <div>{title}</div>

我也遇到过这个问题。

const handleLogout = () => {
router.push('/')
} 

上面的代码给了我同样的问题,然后代码更新如下,然后错误在我的情况下消失

const handleLogout = (): void => {
router.push('/')
}

参考链接: https : //github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-function-return-type.md

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM