簡體   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