TypeScript Analyzer (ESLint, Prettier)

A linter and formatter for JavaScript and TypeScript in Visual Studio

Linting TypeScript using Rules that Need Type Information

Background: Type-Aware Rules and the TypeScript ESLint Plugin

The TypeScript ESLint plugin has two sorts of rules for linting TypeScript:

For the rules that require type information to work you have to be using tsconfig.json files to structure your project, and you have to tell the linter about the tsconfig.json files.

Default TypeScript Analyzer Behavior

By default the Analyzer only uses TypeScript rules that do NOT need type information. If you include a rule in your default configuration file (.eslintrc.js file) that needs type information without doing anything else you will get an error as below:

‘ESLint error caused by a rule included in config that needs type information, but use of tsconfig not enabled.’

Making the TypeScript Analyzer Work with Type-Aware Rules

You have two options to make a rule with type information work, assuming you have appropriate tsconfig.json files set up:

  1. Easiest is to tell the Analyzer to use tsconfig files, and let it do the rest of the work for you. To do this set ‘Tools/Options/TypeScript Analyzer/TypeScript/TypeScript: lint using tsconfig.json’ to True. For any subsequent lint of TypeScript files the Analyzer will attempt to find appropriate tsconfig.json files and give them to the linter. A drawback is that it may get this wrong if you have an unusual set-up. We have documented the rules the Analyzer uses to find tsconfig.json files.
  2. Alternatively you can explicitly tell the linter which tsconfig.json file to use by setting up local .eslintrc.* configuration file with a parserOptions section. This is documented on the TypeScript ESLint plugin page on linting with type information.

A full example of linting with a rule that needs type information is below. This shows how to use both of the options above.

Advantages of a Configuration File

Whilst fixing the problem with a configuration file (option 2 above) is more difficult, it has some advantages:

Example of How to Lint with a Rule that Needs Type Information

This example sets up a Node TypeScript Console Application in Visual Studio, and enables the rule ‘unbound-method’ that needs type information. We do this for both of the options described above for getting the rules to work: setting the flag in Tools/Options or editing a .eslintrc.js file.

Basic Set Up

We’ve temporarily broken all linting: ESLint is very unforgiving of config problems and will just throw if it finds something wrong. We want the code above to correctly generate the unbound-method linting error.

As described above, there are two ways of fixing the webserver error:

Fixing with the Analyzer Flag

Fixing with a Configuration File