Ale*_*vey 22 javascript typescript reactjs eslint typescript-eslint
@typescript-eslint/no-unused-vars存在一个问题。所以,我们有类型
type SomeType = (name: string) => void;
Run Code Online (Sandbox Code Playgroud)
我们在带有类型声明的字符串中有@typescript-eslint/no-unused-vars错误,它说 'name' is defined but never used。
类型用法示例:
export const LogSomeInfo: SomeType = (name: string) => {
const a = name;
console.log(a);
};
Run Code Online (Sandbox Code Playgroud)
或者:
interface CheckboxPropsType {
value: string,
onClick(value: string): void
}
Run Code Online (Sandbox Code Playgroud)
并且 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 打字稿": "^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"
}
}
Run Code Online (Sandbox Code Playgroud)
Bra*_*her 23
来源:我是该typescript-eslint项目的维护者。
如果您将@typescript-eslint/parser和版本更新@typescript-eslint/eslint-plugin到 v4.1.0,您将能够使用最新的更改,这些更改可以@typescript-eslint/no-unused-vars在所有情况下正常工作。
顺便说一句 - 使用插件的 v3.x 但解析器的 v4.x 会让你进入一个非常奇怪的状态,具有未定义和不受支持的行为。
您应该确保始终使用两个软件包的相同版本,因为每个版本都是一起发布的。
Dol*_*lly 16
之前检查过的类似问题:Why ESLint throws 'no-unused-vars' for TypeScript interface?
禁用'@typescript-eslint/no-unused-vars': 0规则,而使用"strict": true,"noUnusedLocals": true以及"noUnusedParameters": true在tsconfig.json
小智 8
只需确保您拥有以下最新版本:
在.eslintrc.js中,确保将以下行添加到规则部分:
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11250 次 |
| 最近记录: |