How to Auto Fix Lint on Save with VS Code TSLint Extension
- By : Mydatahack
- Category : Node.js, Web Technologies
- Tags: TSLint, TypeScript, VS Code
Wouldn’t you rather spend time coding than fixing pesky lint errors? The best time saver for developing TypeScript apps is auto-fixing lint on save. This is a quick instruction to endow this magical power to your VS code.
Steps
(1) Install tslint in your project.
npm i –save-dev tslint
(2) Add tslint.json file into your root folder
You can define whatever linting rules you want to use in this file. Here is the example.
{ "rules": { "no-trailing-whitespace": [true, "always"], "prefer-const": [true, {"destructuring": "any"}], "semicolon": [true, "always"], "space-before-function-paren": [true, "never"], "quotemark": [true, "single"] } }
(3) Install VS code TS Lint Extension
I use the deprecated version, TSLint (deprecated) because it does the auto fix on save. In the future, the newer one will probably have the same functionality. Until then, this one is better!
(4) Set tslint.autoFixOnSave to true
Go to File > Preference > Settings. Search for tslint. You will see TSLint: Auto Fix On Save will come up under UserSetting. Click Edit in settings.json.
Set tslint.autoFixOnSave to true
That’s it. Now the VS code fixes lint on save automatically according to your definition in tslint.json.