Sub topic 16
Linting TypeScript with typescript-eslint
This chapter assumes you already have a working eslint.config.js flat config, know how rule severities work, and have a TypeScript project with a tsconfig.json. ESLint's default parser only understands JavaScript. The typescript-eslint project adds two things: a parser that turns TypeScript syntax into an AST ESLint can walk, and a plugin of rules written for TypeScript.
Installing the packages
The typescript-eslint package bundles the parser, the plugin, and ready-made configs:
npm install --save-dev eslint @eslint/js typescript typescript-eslintA basic config
// eslint.config.mjs
import js from "@eslint/js";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier";
export default [
js.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
"@typescript-eslint/no-explicit-any": "warn",
},
},
eslintConfigPrettier,
];tseslint.configs.recommended is an array, so you spread it into the export. It sets the parser for .ts, .tsx, .mts, and .cts files and turns on the plugin's rules. It also switches off core rules that the TypeScript compiler already covers, such as no-undef. Keep eslint-config-prettier last, as in earlier chapters.
Plugin rules carry the @typescript-eslint/ prefix. Some of them replace core rules. For example, @typescript-eslint/no-unused-vars understands type-only imports, and the recommended config enables it in place of no-unused-vars.
Typed linting
Some bugs only show up when the linter can see types. A promise that nobody awaits is one example. Rules that catch these bugs need type information from the TypeScript compiler:
// eslint.config.mjs
import js from "@eslint/js";
import tseslint from "typescript-eslint";
export default [
js.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ["**/*.js", "**/*.mjs"],
...tseslint.configs.disableTypeChecked,
},
];projectService: true tells the parser to find the nearest tsconfig.json for each file. import.meta.dirname requires Node.js 20.11 or later. The last block turns typed rules off for plain JavaScript files, including the config file itself.
With this config, the code below reports @typescript-eslint/no-floating-promises:
async function save(): Promise<void> {}
save(); // error: Promises must be awaitedCommon errors
| Symptom | Cause | Fix |
|---|---|---|
| `Parsing error: Unexpected token :` | `.ts` files parsed by the default parser | Add `tseslint.configs.recommended` |
| "file was not found by the project service" | The file is not included in `tsconfig.json` | Add it to `include`, or apply `disableTypeChecked` to it |
| Linting is much slower | Typed rules build a full type program | Use typed configs only where you need them |
Exercise
You enabled recommendedTypeChecked, and now ESLint reports a parsing error for eslint.config.mjs saying it is not part of any TypeScript project. What are two ways to fix it?
Show answer
Add a block with files: ["**/*.js", "**/*.mjs"] that spreads tseslint.configs.disableTypeChecked, so typed rules skip JavaScript files. Alternatively, include the file in the TypeScript project, for example by adding it to include in tsconfig.json with allowJs enabled.
Exercise
Why does tseslint.configs.recommended need the spread operator (...) in the exported array, while js.configs.recommended does not?
Show answer
tseslint.configs.recommended is an array of config objects. js.configs.recommended is a single object. Spreading puts each object from the typescript-eslint array into the top-level array as its own entry.