Visual Studio Code with NPM and TypeScript (Part 5 : TsLint)<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Visual Studio Code with NPM and TypeScript (Part 5 : TsLint)

Posted on: March 13, 2017

Code needs to have some consistency to be able to have a uniform reading later. When your code grows and the number of software developers, it becomes harder to handle it manually. TsLint is a TypeScript linter who is a code analyzer that gives which line needs improvement in term of code readability, maintainability, and functionality errors. TsLint is configurable for your own preferences and can be executed manually, automatically when writing code or as a step in your continuous integration process.

To install TsLint, you need to install the TsLint npm package:

1npm install -g tslint typescript --savedev npm install gulp-tslint --savedev

You have to create a configuration file "tslint.json" at the root of your project. The position of this file is the same as the tsconfig.json or typings.json or gulpfile.js file.

In this file, you can configure the rules you want. A basic configuration is to extend an existing one and override some properties to your taste.

2 /*
3 * Possible values:
4 * - the name of a built-in config
5 * - the name of an NPM module which has a "main" file that exports a config object
6 * - a relative path to a JSON file
7 */
8 "extends": "tslint:latest", "rules": {
9 /*
10 * Any rules specified here will override those from the base config we are extending.
11 */
12 "curly": true,
13 "triple-equals": true
14 },
15 "jsRules": {
16 /*
17 * Any rules specified here will override those from the base config we are extending.
18 */
19 "curly": true
20 },
21 "rulesDirectory": [
22 /*
23 * A list of relative or absolute paths to directories that contain custom rules.
24 * See the Custom Rules documentation below for more details. *
25 */
26 ]

In the example above, we extend "latest". You can read the configuration in their official GitHub:

If we want to have the rule to be executed, we need to use TsLint extension for VsCode.


At this point, you may have to reboot VsCode to have the extension to be executed when you type in Visual Studio Code. Once restarted, you should see :


The green lines indicate that the import is missing a semi-colon, that the var should be let/const and that a triple equals should be used.

Next step, is to have a Gulp task that can be executed. It can be a good idea to have this task ran automatically with your task that will build, run test, lint code. To do so, we need to use the gulp-tslint module.

1const gulp_tslint = require('gulp-tslint');

The task needs to get all TypeScript files, but skip definition files and node_modules.

1gulp.task('tslint', () => {
2 return gulp.src(['**/*.ts', '!**/*.d.ts', '!node_modules/**'])
3 .pipe(gulp_tslint())
4 .pipe(;
5 });

To use, in the console use : gulp tslint and you will get a report that looks like below:

1[20:08:53] Using gulpfile D:\\code\\tsWithCode1\\gulpfile.js
2 [20:08:53] Starting 'tslint'... D:/code/tsWithCode1/src/file1.ts[7, 2]: file should end with a newline D:/code/tsWithCode1/src/file1.ts[4, 1]: Forbidden 'var' keyword, use 'let' or 'const' instead D:/code/tsWithCode1/src/file1.ts[1, 41]: Missing semicolon D:/code/tsWithCode1/src/file1.ts[5, 7]: == should be ===
4D:/code/tsWithCode1/src/fileToInclude.ts[9, 2]: file should end with a newline D:/code/tsWithCode1/src/fileToInclude.ts[4, 9]: Calls to 'console.log' are not allowed. D:/code/tsWithCode1/src/fileToInclude.ts[3, 5]: misplaced opening brace D:/code/tsWithCode1/src/fileToInclude.ts[5, 13]: Identifier 'div' is never reassigned; use 'const' instead of 'let'.

From this point, you can increase the amount of rules and have a more rigid code or less depending of your need. Do not forget to add into your repository the configuration to be sure that everyone of the team uses the same rules.