Sub topic 3
How the typescript-eslint Parser and Plugin Fit Together
This chapter assumes you know basic TypeScript syntax, have a tsconfig.json, and can install npm packages. You should also be comfortable with ES module imports and exports.
ESLint checks source code against rules. To check a file, ESLint first parses the file into an abstract syntax tree, or AST. An AST is a structured representation of the code. Rules inspect this structure instead of reading the source as plain text.
TypeScript needs an extra layer because it adds syntax that standard JavaScript parsers do not understand. The typescript-eslint project supplies that layer and also supplies rules designed for TypeScript.
The parser and plugin have different jobs
The typescript-eslint parser reads TypeScript syntax and produces an AST that ESLint can process. It understands features such as:
- Type annotations
- Interfaces and type aliases
- Enums
- Generics
- Type assertions
as constexpressions
The typescript-eslint plugin provides ESLint rules. These rules understand TypeScript-specific patterns and replace or supplement some core ESLint rules.
| Part | Main job | Example |
|---|---|---|
| Parser | Reads TypeScript and produces an AST | Understands `const id: number = 1` |
| Plugin | Adds TypeScript-aware rules | `@typescript-eslint/no-explicit-any` |
| ESLint | Runs rules and reports violations | Displays errors in an editor or CI |
A parser does not automatically enable rules. A plugin does not parse files by itself. ESLint needs both parts for TypeScript-aware linting.
Registering both parts in flat config
In a flat configuration file, you can register the parser and plugin explicitly:
// eslint.config.mjs
import { defineConfig } from 'eslint/config';
import tseslint from 'typescript-eslint';
export default defineConfig({
files: ['**/*.ts', '**/*.tsx'],
languageOptions: {
parser: tseslint.parser,
parserOptions: {
sourceType: 'module',
},
},
plugins: {
'@typescript-eslint': tseslint.plugin,
},
rules: {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-unused-vars': 'warn',
},
});The files property limits this configuration to TypeScript files. languageOptions.parser tells ESLint how to read those files. The plugins object gives the plugin a name. Rule names use that name as their prefix.
For most projects, a preset is easier than registering every part manually:
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
);The recommended typescript-eslint preset supplies the parser, plugin, and a selected group of rules. It also keeps the configuration aligned with the package version.
How a lint run processes a file
A typical run follows this sequence:
Core rules and plugin rules can inspect the same AST. However, a core rule may not understand TypeScript-specific meaning. For example, a JavaScript rule might treat a type-only construct as unexpected. Prefer the corresponding typescript-eslint rule when one exists.
Common configuration errors
A common error is enabling a plugin rule without registering the plugin. ESLint then reports that it cannot find the rule definition. Another error is applying the TypeScript parser to JavaScript files without checking whether those files belong to the intended project.
Check these points when debugging:
- The package
typescript-eslintis installed. - The configuration applies to the target file.
- The plugin name matches the prefix in each rule name.
- TypeScript files use
tseslint.parser, directly or through a preset. - You are not mixing an old
.eslintrcexample with a flat config file.
Exercise
What is the difference between the typescript-eslint parser and plugin?
Show answer
The parser converts TypeScript source into an AST that ESLint can read. The plugin adds rules that inspect that AST and understand TypeScript-specific code.