【发布时间】:2020-12-25 06:18:43
【问题描述】:
@typescript-eslint/no-unused-vars 存在一个问题。 所以,我们有类型
type SomeType = (name: string) => void;
我们在带有类型声明的字符串中有 @typescript-eslint/no-unused-vars 错误,表示“名称”已定义但从未使用。。 p>
类型使用示例:
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 配置有问题吗?
"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", 4],
"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", 4],
"max-len": ["error", { "code": 120 }],
"react/prop-types": "off"
}
}
【问题讨论】:
-
为什么选择 eslint-plugin 3.6.1 和 parser 4.0.1?您是否尝试将两者更新到相同的版本(例如 4.0.1)?
-
遇到了具有相同症状的不同问题。我们使用
no-unused-vars而不是@typescript-eslint/no-unused-vars。将 @typescript-eslint/eslint-plugin 和 @typescript-eslint/parser 升级到 4.9.0 并将以下内容添加到 .eslintrc 解决了这个问题:"no-unused-vars": 0, "@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }]
标签: javascript reactjs typescript eslint typescript-eslint