Search code examples
angularimporttslintangular14linter

How do I run tslint against a single file?


I'm using Angular 14. I have set up a rule in tslint to warn about unused imports, and I can fix this across my project by running

> tslint --config tslint-imports.json --fix --project .

However, what if I just want to run the fix on a single file? I tried this

> tslint --config tslint-imports.json --fix ./src/app/services/my-products.service.ts
Warning: The 'no-unused-declaration' rule requires type information.

but besides the warning above, the file itself isn't fixed (it is fixed when it is run as part of the larger project initiative in the first code line above)

Edit: tslint-imports.json config file is

{
        "extends": [
                "tslint-etc"
        ],
        "rules": {
                "no-unused-declaration": true
        }
}

Solution

  • Please note that tslint was deprecated 3 years ago. Why did you use it together with Angular 14?

    Switch to eslint instead.
    Please be aware that eslint does not have a feature like no-unused-declaration and it is not planned to implement this.

    But with some some extra plugins it is possible:

    eslint-plugin-import can do this with import/no-unused-modules:

    {
     plugins: ['@typescript-eslint', 'import'],
     rules: {
       'import/no-unused-modules': [2, { unusedExports: true }],
     },
    };
    
    

    Source: Github comment by golopot