Sub topic 3
How the typescript-eslint parser and plugin fit together
This chapter assumes you can read TypeScript and a tsconfig.json, run npm scripts, and write ES module imports. It also assumes you have seen an ESLint flat config file. Here you will learn what the two typescript-eslint pieces do, how they connect inside a config, and which errors appear when one piece is missing.
Why ESLint needs two extra pieces
ESLint on its own understands JavaScript. It reads your source, turns it into a tree of nodes, and runs rules against that tree. That tree is called an AST (abstract syntax tree). ESLint's default parser, called Espree, cannot read type annotations, interfaces, enums, generics, or as expressions. Given a .ts file, it stops with a parsing error.
typescript-eslint fills two gaps with two separate parts:
- The parser (
@typescript-eslint/parser) reads TypeScript source and produces an AST that ESLint can walk. It uses the TypeScript compiler's own parsing, then converts the result to a format called ESTree, which is the tree shape ESLint expects. - The plugin (
@typescript-eslint/eslint-plugin) is a collection of rules. Some rules replace core ESLint rules that give wrong answers on TypeScript code. Others check things that only exist in TypeScript, such as@typescript-eslint/no-explicit-any.
The two parts depend on each other. Plugin rules assume the TypeScript-specific node types that only the parser creates. The parser alone reports nothing, because it contains no rules.
The single package you install
Older setups installed the parser and the plugin as two packages and wired them up by hand. Current setups install one package named typescript-eslint. It re-exports both parts and supplies ready-made configs that wire them together.
npm install --save-dev eslint @eslint/js typescript typescript-eslintThe typescript-eslint package gives you these exports, which you will use in the config file:
| Export | What it is |
|---|---|
| `tseslint.configs.recommended` | A preset that registers the parser and plugin and turns on a set of rules |
| `tseslint.parser` | The parser object, for when you wire it by hand |
| `tseslint.plugin` | The plugin object, for when you wire it by hand |
| `tseslint.config` | A helper that builds a config array (ESLint now also offers `defineConfig` for the same job) |
What a preset does for you
The most common config is also the shortest:
// 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,
);tseslint.configs.recommended is an array of several config objects, not one. Each object has a job:
- A base object registers the parser and the plugin. After this object, every rule named
@typescript-eslint/...is available. - An eslint-recommended object turns off core rules that TypeScript already covers, for example rules that check for undefined variables. The compiler does that work better than a lint rule can.
- A rules object turns on the rules that make up the preset.
That is why the short config works. You do not set a parser or list the plugin, because the first object in the preset has already done both.
Wiring the parser and plugin by hand
You rarely need to do this, but doing it once shows what the preset hides. The long form below does the same registration as the base object.
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default [
eslint.configs.recommended,
{
files: ['**/*.ts'],
languageOptions: {
parser: tseslint.parser,
},
plugins: {
'@typescript-eslint': tseslint.plugin,
},
rules: {
'@typescript-eslint/no-explicit-any': 'error',
},
},
];Two keys do the work:
languageOptions.parsertells ESLint which parser to use for the files that the object matches. Without it, ESLint falls back to Espree.pluginsmaps a prefix to a plugin object. The key'@typescript-eslint'is the prefix you write in rule names. If you register the plugin under a different key, the rule names change with it.
The hand-wired form leaves out the object that turns off redundant core rules. If you use this form with eslint.configs.recommended, core rules such as no-undef stay on and report false errors on type names. This is one reason to prefer the preset.
Rules that replace core rules
Some core ESLint rules fail on TypeScript syntax. A well-known case is no-unused-vars. It treats a type-only import or an enum member as unused, or reports the parameter names in a function type. The plugin ships @typescript-eslint/no-unused-vars, which understands TypeScript. These are called extension rules.
The standard pattern is to switch off the core rule and switch on the extension, so one rule does the work and you do not get duplicate reports. The recommended preset already does this. If you set rules yourself, you must do both steps:
// additional entry in the flat config array
{
files: ['**/*.ts'],
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
},What the parser needs in order to see types
By default the parser reads each file on its own. It sees the syntax of that one file and nothing else. It cannot tell what a type from another file looks like. Rules that depend only on syntax work fine in this mode. no-explicit-any is one of them, because the word any is visible in the file.
Other rules need to know the type of an expression. To get this, the parser must build a TypeScript program for your project, which means it reads your tsconfig.json. You turn this on with the parserOptions key that the next chapters cover in detail:
// 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.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
);Note the split of duties. The parser object is still the same one. What changes is how much information it hands to the rules: the plugin's type-checked rules ask the parser for the TypeScript type checker and use it to answer questions about types. Syntax-only rules keep working as before.
Common errors and what they mean
The wiring between parser and plugin fails in a few typical ways. Each has a clear signature.
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `Parsing error: Unexpected token` on a type annotation | The file is parsed by Espree, so the parser was not set for this file | Use a preset, or set `languageOptions.parser` for a `files` pattern that includes the file |
| `Definition for rule '@typescript-eslint/...' not found` | The plugin is not registered, or it is registered under another key | Use a preset, or add `plugins: { '@typescript-eslint': tseslint.plugin }` |
| `You have used a rule which requires type information, but don't have parserOptions set` | A type-checked rule is on but the parser has no project | Add `projectService: true` and `tsconfigRootDir` |
| `was not found by the project service` | The file is not covered by any `tsconfig.json` | Add the file to `include` in a tsconfig, or lint it with the `disableTypeChecked` config |
| Reports for `no-undef` on type names | The preset step that disables redundant core rules is missing | Use `tseslint.configs.recommended` rather than a hand-built object |
The fourth row shows up often with files such as eslint.config.mjs itself. These JavaScript files sit outside the TypeScript project, so the type-checked rules cannot work on them. The fix is to apply the disableTypeChecked config to them:
// additional entry in the flat config array
{
files: ['**/*.js', '**/*.mjs'],
extends: [tseslint.configs.disableTypeChecked],
},Checking that the wiring works
You can confirm that the parser and plugin are active without waiting for a real bug. Create a file with a violation that only the plugin can see, then lint it.
// check.ts
export function read(input: any): string {
return String(input);
}Run ESLint on it:
npx eslint check.tsWith the recommended preset, you should see @typescript-eslint/no-explicit-any reported on the any annotation. If you see a parsing error instead, the parser is not attached to the file. If you see no report at all, check that the files patterns in your config match check.ts and that nothing later in the array turns the rule off.
To see exactly what ESLint resolved for a file, print its config:
npx eslint --print-config check.tsThe output lists the parser, the registered plugins, and every rule setting that applies to that file. Reading it is the fastest way to find out which config object won.
Order and precedence in the array
A flat config is an array, and ESLint applies the objects in order. When two objects match the same file and set the same option, the later object wins. This matters for the parser and plugin in two ways:
- If you put your own rules before a preset, the preset can override your severities. Place your overrides after the presets.
- If a later object sets
languageOptions.parserfor the same files, it replaces the parser that the preset set. Do this only when you mean to use a different parser, such as one for a framework's template language.
Exercise
Your config uses eslint.configs.recommended and a hand-written object that sets languageOptions.parser to tseslint.parser and registers the plugin. Lint output now reports no-undef errors on the name of an interface used in a type annotation. What is missing, and what is the simplest fix?
Show answer
The hand-written object does not include the step that disables core rules that TypeScript already covers, so no-undef still runs and does not understand type names. The simplest fix is to add tseslint.configs.recommended (which includes that step along with the parser and plugin) in place of the hand-written object. Alternatively, set 'no-undef': 'off' for the TypeScript files, but the preset also handles other overlapping rules.
Exercise
A teammate sees the error "Definition for rule '@typescript-eslint/no-floating-promises' not found" even though they installed the typescript-eslint package. Name two likely causes in the config.
Show answer
First, the plugin was never registered, because the config has no preset and no plugins: { '@typescript-eslint': tseslint.plugin } entry. Second, the plugin was registered under a different key, so the rule prefix in the rules block does not match the key. Using a preset avoids both problems.