Angular-cli : Using ng lint

Apparently, Angular 6.0 and newer supports this functionality natively.


For pre-Angular 6.0:

ng lint runs the tslint (TypeScript linter) which just prints out linting errors. AFAIK, it doesn't directly allow fixing found problems (as of Nov 16)

There are projects as tslint-fix which aim for fixing a set of so-called auto-fixable problems.


For Angular 6.0+ you can run ng lint with autofix like so:

ng lint <project> --fix

where <project> is the name you gave to your project when running ng new.

Learn more here: https://github.com/angular/angular-cli/wiki/lint


Updated answer for Angular CLI v6.x, 7.x, 8.x:

ng lint <project-name> --fix

where <project-name> is "name:" from package.json

-- answer for Angular CLI v1.x --

ng lint -fix

-- Original answer below --

To have tslint autofix many errors run the following in the root of your code. Obviously it can only autofix simpler issues like let -> const, "" -> ' etc.

npx tslint src/**/*.ts --fix

Yesterday I did this to auto-fix hundreds of let -> const issues in our fairly large code bases. Just reviewing the changes before committing took long enough, manually fixing them all would have taken over a day.


The functionality you are asking about is partially available these days in VS Code using the TSLint extension which does support Auto Fixing for some (but not all) of the default TSLint warnings.

I've been using it for a few weeks now and I find it quite helpful.