问题描述:
我已经开始使用typescript和eslint开展一个项目。问题是,eslint 给了我一个关于似乎是正确声明的函数的意外错误
代码:
import React, { useState } from 'react';
type Props = {
options: Array<string>
};
const Switch: React.FC<Props> = (props: Props) => {
const [choice, setChoice] = useState<number>(0);
// eslint gives error @ following function
const handleSwitchChange = ():void => {
choice + 1 >= options.length ? setChoice(0) : setChoice(choice + 1)
};
return (
<div>{options[choice]}</div>
<button onClick={handleSwitchChange}>Change choice</button>
);
};
更多详细信息:
现在这可以按预期工作,但尽管我传递给的函数是声明类型void
eslint 给我以下错误:
期望一个赋值或函数调用,而是看到一个表达式。
现在我不想只是禁用规则,但我看不出代码有任何固有的错误。有什么方法可以正确编写函数,这样 es-lint 就不会抱怨了吗?
ESLint 错误(额外信息)
引发错误的特定 eslint 模块
{
"resource": "<PATH CENSORED>/src/components/forms/Switch.tsx",
"owner": "eslint",
"code": "@typescript-eslint/no-unused-expressions",
"severity": 8,
"message": "Expected an assignment or function call and instead saw an expression.",
"source": "eslint",
"startLineNumber": 12,
"startColumn": 5,
"endLineNumber": 12,
"endColumn": 85
}