问题描述
@ typescript-eslint / no-unused-vars 存在一个问题。 所以,我们输入
type SomeType = (name: string) => void;
我们在带有类型声明的字符串中遇到了 @ typescript-eslint / no-unused-vars 错误,其中定义了“名称”,但从未使用。>
类型用法示例:
export const LogSomeInfo: SomeType = (name: string) => {
const a = name;
console.log(a);
};
或者:
interface CheckBoxPropsType {
value: string,onClick(value: string): void
}
eslint在onClick ...字符串处中断,表示该值已定义但从未使用过。 即使正确分配了类型并且实际的onClick处理程序也使用了该值!
问题:此规则有什么问题以及在这种情况下为什么会触发。为什么eslint将类型/接口中的函数的类型声明识别为常规函数?我的eslint配置有问题吗?
“夹板”:“ ^ 7.7.0”, “ @ typescript-eslint / eslint-plugin”:“ ^ 3.6.1”, “ @ typescript-eslint / parser”:“ ^ 4.0.1”, “ eslint-config-airbnb-typescript”:“ ^ 9.0.0”,
{
"extends": [
"airbnb-typescript","airbnb/hooks","plugin:react/recommended","plugin:@typescript-eslint/recommended"
],"parser": "@typescript-eslint/parser","parserOptions": {
"ecmaFeatures": {
"jsx": true
},"ecmaVersion": 2018,"sourceType": "module","project": "./tsconfig.json"
},"settings": {
"react": {
"version": "detect"
}
},"plugins": ["@typescript-eslint","react-hooks","react"],"env": {
"browser": true
},"rules": {
"object-curly-newline": 0,"import/named": ["error"],"indent": ["error",4],"react/jsx-indent": ["error","comma-dangle": ["error","never"],"import/prefer-default-export": "off","react/jsx-fragments": "off","arrow-body-style": "off","object-curly-spacing": "off","@typescript-eslint/indent": ["error",4,{"SwitchCase": 1}],"@typescript-eslint/explicit-module-boundary-types": "off","no-undef": "error","react/jsx-indent-props": ["error","max-len": ["error",{ "code": 120 }],"react/prop-types": "off"
}
}
解决方法
如果将@typescript-eslint/parser
和@typescript-eslint/eslint-plugin
的版本更新为v4.1.0,则将能够使用使@typescript-eslint/no-unused-vars
在所有情况下都能正常工作的最新更改。
顺便说一句-使用插件的v3.x但解析器的v4.x将使您进入一种真正的怪异状态,具有未定义和不受支持的行为。
您应该确保始终使用相同版本的两个软件包,因为每个版本都一起发布。
,类似的问题请早先检查:Why ESLint throws 'no-unused-vars' for TypeScript interface?
在tsconfig.json中,禁用'@ typescript-eslint / no-unused-vars':0,然后改用“ strict”:true,“ noUnusedLocals”:true和“ noUnusedParameters”:true