如何从React Typescript的整个项目中删除未使用的导入/声明? [英] How can I remove unused imports/declarations from the entire project of React Typescript?

查看:832
本文介绍了如何从React Typescript的整个项目中删除未使用的导入/声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除此SO线程中针对角度。我正在尝试使用 eslint-plugin-react 来实现此目标,但不是找到了一个选项,可以用一个命令从整个项目中删除未使用的导入和声明。

I'm trying to remove unused imports and declarations as answered in this SO thread for Angular. I'm trying to achieve the goal using eslint-plugin-react, but not found any option to remove the unused imports and daclarations from the entire project, with a single command.

这是我的.eslintrc.json

Here is my .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"
    }
}

或者有没有办法使用ESLint或VS Code中的Typescript Hero扩展?

Or is there a way to do the same using ESLint or Typescript Hero extensions in VS Code?

推荐答案

eslint 具有内置的修复选项。

eslint ... --fix

如果规则包含 no-unused-variable ,eslint会修复通过删除未使用的导入(以及其他自动修复 )。

If rules contains no-unused-variable, eslint will "fix" the unused import by removing them (along with other auto fixes).

以下是示例存储库,其中-fix 删除了未使用的 import s。

Here is an example repo which --fix removes the unused imports.

https://github.com/moshfeu/eslint-ts-未使用的进口商品

这篇关于如何从React Typescript的整个项目中删除未使用的导入/声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆