Using unused-imports plugin and unused-imports/no-unused-imports-ts rule, eslint --fix will remove the imports.
Here is an example repo which --fix removes the unused imports.
https://github.com/moshfeu/eslint-ts-unused-imports
Setup
yarn add -D eslint-plugin-unused-imports
// eslint.config.js
import tseslint from "typescript-eslint";
import unusedImports from "eslint-plugin-unused-imports";
export default tseslint.config(
{
plugins: {
"unused-imports": unusedImports,
},
rules: {
"unused-imports/no-unused-imports": "warn",
},
}
);
Old answer
eslint has a built-in fix option.
eslint ... --fix
If rules contains no-unused-vars, eslint will "fix" the unused import by removing them (along with other auto fixes).
» npm install eslint-plugin-unused-imports
Using unused-imports plugin and unused-imports/no-unused-imports-ts rule, eslint --fix will remove the imports.
Here is an example repo which --fix removes the unused imports.
https://github.com/moshfeu/eslint-ts-unused-imports
Setup
yarn add -D eslint-plugin-unused-imports
// eslint.config.js
import tseslint from "typescript-eslint";
import unusedImports from "eslint-plugin-unused-imports";
export default tseslint.config(
{
plugins: {
"unused-imports": unusedImports,
},
rules: {
"unused-imports/no-unused-imports": "warn",
},
}
);
Old answer
eslint has a built-in fix option.
eslint ... --fix
If rules contains no-unused-vars, eslint will "fix" the unused import by removing them (along with other auto fixes).
You can also use shortcuts in VSCode, if it needs to be done quickly and without extra effort:
- Option + Shift + O for Mac
- Alt + Shift + O for Windows
» npm install @antfu/eslint-plugin-unused-imports
» npm install eslint-plugin-unused-vars-and-imports
Angular 19 warns about unused imports. Is there a way to remove unused imports specific to Angular templates using ESLint? Note that I’m not referring to TypeScript imports but Angular-specific imports.
» npm install eslint-plugin-known-imports