我正在尝试删除this SO thread中为Angular回答的未使用的导入和声明。我正在尝试使用eslint-plugin-react实现目标,但没有找到任何选项可以通过单个命令从整个项目中删除未使用的导入和声明。
这是我的.eslintrc.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12
},
"plugins": [
"react",
"@typescript-eslint",
"unused-imports"
],
"rules": {
"indent": [
"warn",
"tab"
],
"linebreak-style": [
"warn",
"windows"
],
"quotes": [
"warn",
"double"
],
"semi": [
"warn",
"always"
],
"@typescript-eslint/no-unused-vars": "on",
"unused-imports/no-unused-imports-ts": "on"
}
}
还是有一种方法可以在VS Code中使用ESLint或Typescript Hero扩展来做同样的事情?
最佳答案
使用 unused-imports
插件和unused-imports/no-unused-imports-ts
规则,eslint --fix
将删除import
。
这是一个示例 repo ,--fix
删除了未使用的import
。
https://github.com/moshfeu/eslint-ts-unused-imports
旧答案
eslint具有内置的fix选项。
eslint ... --fix
如果rules
包含no-unused-vars
,则eslint会通过删除未使用的导入(以及其他auto fixes)来“修复”未使用的导入。
关于reactjs - 如何从React Typescript的整个项目中删除未使用的导入/声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64365300/