Sub topic 16
Linting TypeScript with typescript-eslint
This chapter assumes you have finished the earlier chapters. You should know how a flat config file (eslint.config.js) works, what severity levels mean, and how eslint-config-prettier fits in. You also need a project with TypeScript installed and a tsconfig.json. You do not need to know TypeScript's type system in depth. Reading basic type annotations is enough.
Why ESLint Needs Help with TypeScript
ESLint's default parser, Espree, only understands JavaScript. Give it a file containing let count: number = 0 and it stops with a parsing error at the colon. ESLint's core rules also know nothing about types. They cannot tell whether a value is a Promise, a string, or any.
typescript-eslint fills both gaps. It is a set of packages that ships as one npm package, typescript-eslint:
- A parser turns TypeScript source into an ESTree-compatible AST. ESTree is the tree format ESLint rules expect, so core rules keep working.
- A plugin adds rules under the
@typescript-eslint/prefix. Some rules read only syntax. Others ask the TypeScript compiler for type information. - Shared configs bundle those rules, like
js.configs.recommendeddoes for core rules.
ESLint does not replace the TypeScript compiler. typescript-eslint never reports type errors such as passing a number where a string is expected. You still need tsc --noEmit (or your build tool) for type checking. ESLint catches patterns that type-check fine but are probably bugs.
Installing and a Minimal Config
Install ESLint, the core rules, TypeScript, and typescript-eslint as dev dependencies:
npm install --save-dev eslint @eslint/js typescript typescript-eslintThen write the config. Name it eslint.config.mjs, or keep .js if package.json has "type": "module".
// eslint.config.mjs
import js from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
export default defineConfig(
js.configs.recommended,
tseslint.configs.recommended,
);defineConfig comes with ESLint 9.22 and later. It flattens nested arrays, which matters here because tseslint.configs.recommended is an array of config objects, not a single object. Older guides use tseslint.config(...) instead. It does the same job but typescript-eslint has deprecated it in favor of defineConfig.
The recommended config does three things:
- It sets the typescript-eslint parser for
.ts,.tsx,.mts, and.ctsfiles, and adds those extensions to whateslint .lints. - It turns off core rules that TypeScript already covers or gets wrong, such as
no-undef. - It turns on
@typescript-eslint/*rules, including TS-aware replacements like@typescript-eslint/no-unused-vars.
Try it on a sample file:
// src/user.ts
interface User {
id: number;
name: string;
}
export function greet(user: User): string {
const unused = 42;
const data: any = user;
return `Hello, ${data.name}`;
}Running npx eslint src/user.ts reports two problems:
src/user.ts
7:9 error 'unused' is assigned a value but never used @typescript-eslint/no-unused-vars
8:15 error Unexpected any. Specify a different type @typescript-eslint/no-explicit-anyChoosing a Shared Config
typescript-eslint ships several configs. You can stack them.
| Config | What it adds | Needs type info |
|---|---|---|
| `recommended` | Rules that catch likely bugs, with few false positives | No |
| `strict` | `recommended` plus more opinionated bug-catching rules | No |
| `stylistic` | Consistent TypeScript idioms, such as `interface` vs `type` | No |
| `recommendedTypeChecked` | `recommended` plus type-aware rules | Yes |
| `strictTypeChecked` | `strict` plus type-aware rules | Yes |
| `stylisticTypeChecked` | `stylistic` plus type-aware rules | Yes |
Pick one from the recommended/strict family. Optionally add one stylistic config. stylistic covers code conventions, not whitespace, so it does not conflict with Prettier. Keep eslint-config-prettier as the last entry, as in earlier chapters.
Typed Linting
The most valuable rules need type information. For example, @typescript-eslint/no-floating-promises flags a promise that nobody awaits or handles. A syntax-only rule cannot know whether save() returns a promise. The compiler can.
// eslint.config.mjs
import js from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier";
export default defineConfig(
js.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ["**/*.js", "**/*.mjs", "**/*.cjs"],
extends: [tseslint.configs.disableTypeChecked],
},
{
rules: {
"@typescript-eslint/no-explicit-any": "warn",
},
},
eslintConfigPrettier,
);Each part does the following:
projectService: truetells the parser to find the nearesttsconfig.jsonfor each file, the same way your editor does.tsconfigRootDiranchors that lookup to the config file's folder.import.meta.dirnameneeds Node.js 20.11 or later.- The
disableTypeCheckedblock turns off type-aware rules for plain JavaScript files, such aseslint.config.mjsitself. Those files may not be part of any TypeScript project. - The last rules block shows that you override typescript-eslint rules the same way as core rules, by their prefixed names.
Now this code is flagged:
// src/save.ts
async function save(): Promise<void> {
// write to disk
}
export function onClick(): void {
save();
}src/save.ts
6:3 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promisesFix it with await save() inside an async function, by adding .catch(...), or by writing void save(); to show the fire-and-forget is deliberate.
Trade-offs and Common Errors
The errors you will hit most often:
- "file was not found by the project service" or "not included in any tsconfig". The file is outside the
includepatterns of everytsconfig.json. Add it toinclude, or for a few loose files list them inprojectService: { allowDefaultProject: ["*.js"] }. - "Parsing error: Unexpected token :". Your TypeScript file reached Espree. Usually a
filespattern sends.tsfiles to a config object without the typescript-eslint parser, or the typescript-eslint config is missing. - Duplicate unused-variable reports. You enabled core
no-unused-varsafter the typescript-eslint config. Leave the core rule off for TypeScript files and configure@typescript-eslint/no-unused-varsinstead. - "You have used a rule which requires type information". You turned on a type-aware rule without setting
projectService, or applied it to files covered bydisableTypeChecked. - Unsupported TypeScript version warning. typescript-eslint supports a fixed range of TypeScript versions. A newer compiler usually works, but update typescript-eslint to get rid of the warning and avoid wrong parses.
Exercise
Your teammate adds tseslint.configs.recommended and expects ESLint to report const n: number = "five";. Nothing is reported. Why, and what should catch it?
Show answer
typescript-eslint does not report type errors. That assignment type-checks as an error in the TypeScript compiler, not in a lint rule. Run tsc --noEmit (for example as an npm script and a CI step) alongside ESLint.
Exercise
After switching to recommendedTypeChecked, ESLint fails on eslint.config.mjs with an error saying the file is not part of any TypeScript project. Give two ways to fix it.
Show answer
Add a config object that applies tseslint.configs.disableTypeChecked to JavaScript files (files: ["**/*.js", "**/*.mjs", "**/*.cjs"]). Or keep type checking for it and set projectService: { allowDefaultProject: ["eslint.config.mjs"] }. Adding the file to a tsconfig include also works.
Exercise
Write the line that silences @typescript-eslint/no-floating-promises for sendAnalytics(); without an inline disable comment, while stating that ignoring the promise is intentional.
Show answer
void sendAnalytics(); The void operator marks the promise as deliberately ignored, and the rule accepts it.