TypeScript Analyzer (ESLint, Prettier)

A linter and formatter for JavaScript and TypeScript in Visual Studio

Configuration

Default Configuration

The TypeScript Analyzer will run after installation with no additional configuration necessary to get it working.

Of course ESLint has quite complex configuration options. One of the aims of the TypeScript Analyzer is to allow you to lint and format without necessarily having to understand all that.

To achieve this the TypeScript Analyzer uses a default configuration file (.eslintrc.js) in its own folder (c:\Users\{username}\TypeScriptAnalyzerConfig). This configuration file is used for all Visual Studio linting and formatting by the TypeScript Analyzer unless you override it by providing your own file locally for a specific solution or project or even folder.

The default configuration file can be viewed and edited in Visual Studio via a menu option: Tools/TypeScript Analyzer (ESLint)/Edit Default Config.

Enabling and Disabling Rules

In the default configuration file all rules are listed separately, with a link to the associated documentation. Here you can disable a rule you don’t like. You can also change a rule from generating errors so that it only generates warnings.

For example, let’s say you don’t like the no-debugger rule, which by default generates errors if you put a debugger statement in your code. To change this:

If a rule has possible options then the ESLint syntax is to replace “error”/”warn”/”off” with an array whose first element is the old “error”/”warn”/”off” setting, and subsequent elements are the options. This is just how ESLint expects configuration files to work. For example, the eqeqeq rule has a ‘smart’ option, and we’d set that with code "eqeqeq": ["error", "smart"].

Enabling and Disabling Prettier

See the documentation on Prettier for how to disable Prettier and fall back to simpler formatting rules, or to disable formatting altogether.

Adding a Rule

You may want to add a rule that isn’t in the set of recommended rules.

For example the TypeScript ESLint plugin has an ‘array-type’ rule. This controls what syntax you can use for declaring array types. This rule applies to TypeScript type annotations, which means that it won’t work in JavaScript. So we want to set it up to apply to TypeScript files only.

To add this rule:

"@typescript-eslint/array-type": "warn",

Restoring the Default Configuration File

You can restore the default configuration file to its original contents, as well as resetting any changes to the Tools/Options/TypeScript Analyzer/ESLint settings, with the menu option Tools/TypeScript Analyzer (ESLint)/Reset TypeScript Analyzer (ESLint) Settings…

Local Configuration

You can override the default configuration file by providing a configuration file locally in any of the formats that ESLint supports.

Information on providing your own local configuration is available in a separate article.

Ignoring Files

You can configure the TypeScript Analyzer to ignore files in the same way that ESLint does. More documentation on this is also available.