Sub topic 1
What Linting Does and Why TypeScript Needs typescript-eslint
Prerequisites
This chapter assumes you already have the following:
- Working knowledge of TypeScript syntax and a
tsconfig.jsonfile. - Basic use of npm and Node.js on the command line.
- Familiarity with ES modules (
importandexport).
You do not need any prior ESLint experience. This chapter builds the mental model that the rest of the tutorial relies on. It covers what a linter is, what ESLint can and cannot do with TypeScript on its own, and what typescript-eslint adds.
What linting does
A linter is a program that reads source code without running it and reports patterns that are likely bugs, unsafe practices, or departures from a team's conventions. This is called static analysis. ESLint is the standard linter for JavaScript, and it is also the engine that typescript-eslint builds on.
ESLint works in three steps:
- A parser turns your source text into an abstract syntax tree (AST). The AST is a tree of nodes such as
VariableDeclaration,CallExpressionandIfStatement. - ESLint walks the tree. Each rule subscribes to the node types it cares about and inspects them.
- When a rule finds a problem, it reports it with a location and a message. Some rules also attach a fix, which is an automatic edit that ESLint can apply.
Each rule is a small, independent check. You turn rules on or off and set each one's severity: "off", "warn" or "error". This is what makes ESLint configurable. It is also why the choice of parser matters: rules can only inspect what the parser put into the tree.
Linting is not compiling or formatting
Three tools often run side by side in a TypeScript project, and they answer different questions.
| Tool | Question it answers | Example finding |
|---|---|---|
| TypeScript compiler (`tsc`) | Is this program type-correct? | `string` is not assignable to `number` |
| ESLint with typescript-eslint | Is this code risky or inconsistent, even though it compiles? | A Promise is created and never awaited |
| Formatter (such as Prettier) | Is the layout consistent? | Wrong indentation or quote style |
The compiler checks that types line up. A linter catches code that is type-correct but suspicious. For example, calling an async function without await compiles without complaint, yet the error handling and ordering are probably wrong. Formatting is a separate concern, and a later chapter covers how to keep it apart from linting.
Why plain ESLint is not enough for TypeScript
ESLint was designed for JavaScript. Out of the box, it fails on TypeScript in two ways.
It cannot parse TypeScript syntax
ESLint's default parser understands standard JavaScript only. Give it a type annotation and it stops:
const count: number = 1;Linted with the default parser, this file produces a parsing error similar to Parsing error: Unexpected token :. The same happens with interface, enum, generics, as assertions, decorators and every other TypeScript-only construct. If the parser fails, no rule runs on that file.
Its rules do not know TypeScript semantics
Even with a parser that accepts TypeScript, the built-in rules were written for JavaScript. They assume every identifier is a runtime value. In TypeScript, a name can exist only in the type world, such as an interface or a type alias. A rule such as no-undef can report a type name as undefined, because it looks for a runtime declaration and finds none. Rules that track unused variables, redeclarations or shadowing can likewise misjudge function overloads, enums and declaration merging, because those patterns have no JavaScript equivalent.
There is also a deeper limit. Plain ESLint sees the shape of the code but not its types. It cannot tell whether value is a string, a Promise<void> or any. Many valuable checks depend on exactly that information.
What typescript-eslint adds
The typescript-eslint project fills these gaps with a small set of parts that work together.
- The parser (
@typescript-eslint/parser) reads TypeScript and produces an AST in ESLint's expected format, called ESTree, extended with nodes for TypeScript constructs. The parser can also ask the TypeScript compiler for type information. - The plugin (
@typescript-eslint/eslint-plugin) supplies rules written for TypeScript. Some replace base rules with versions that understand types and TypeScript syntax. Others check things that only exist in TypeScript, such as unsafe use ofany. - Shared configs are presets that bundle a parser setting, a plugin registration and a list of rules into one ready-to-use object.
- The
typescript-eslintpackage is a single convenience import that exposes the parser, the plugin and the presets together. This tutorial uses it throughout.
The diagram shows how a file moves through the pipeline.
The dashed lines mark the optional step. Type information is not always loaded, because producing it is expensive. Rules that do not need it run on the AST alone, and rules that need it only work when you enable type-aware linting. That split between syntax-only and type-aware rules appears in the preset names later in this tutorial, and one chapter is devoted to it.
A first working setup
Install ESLint, its JavaScript rule set, and typescript-eslint. TypeScript itself must also be installed in the project, because the parser and the type-aware rules call into it.
npm install --save-dev eslint @eslint/js typescript-eslint typescriptThen create eslint.config.mjs in the project root. This is ESLint's flat config format, which is covered in the next chapter.
// 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 config does two things. eslint.configs.recommended enables ESLint's own core rule set. tseslint.configs.recommended registers the TypeScript parser and plugin and turns on the TypeScript rules that the project considers broadly useful. It also switches off base rules that would misbehave on TypeScript code.
Run the linter from the project root:
npx eslint .
npx eslint . --fixThe first command reports problems. The second applies the automatic fixes that rules provide. Only rules that attach a fix can be corrected this way. The rest need a manual change.
What a real finding looks like
Here is a file that compiles cleanly but that the recommended preset flags:
export function describe(value: any): string {
return String(value);
}The @typescript-eslint/no-explicit-any rule reports the any annotation. The rule is a good illustration of the division of labor. The compiler accepts any by design, but any switches off type checking for everything it touches, and many teams treat that as a defect. The linter surfaces it without changing what the compiler allows. One fix is to use unknown and narrow it:
export function describe(value: unknown): string {
return String(value);
}Common errors at this stage
These are the problems learners most often hit on a first setup.
- Parsing errors on
.tsfiles. The config does not register the TypeScript parser. Check thattseslint.configs.recommendedappears in the exported config array. - ESLint finds no config. Flat config looks for
eslint.config.js,eslint.config.mjsoreslint.config.cjsin the working directory. Run the command from the project root, and check the file name. Cannot use import statement outside a module. A.jsconfig in a CommonJS package cannot useimport. Name the fileeslint.config.mjs, or set"type": "module"inpackage.json.- Rules that need types do nothing, or throw. The recommended preset does not need type information. Rules from the type-checked presets do, and they fail until you configure a TypeScript project. A later chapter covers this setup.
- Both ESLint and the compiler report the same issue. This is expected for some checks, such as unused variables. A rule is not wrong because
tscoverlaps with it, but you can decide which tool owns a check and turn the duplicate off.
Where the rest of the tutorial goes
The following chapters build on this model in order. They cover flat config structure, the parser and plugin in more detail, the preset choices (recommended, strict and stylistic), tuning severity and options, scoping settings to files, and type-aware linting with the project service. They then turn to inline suppression, the split between linting and formatting, editors and CI, performance, custom rules and migration from the older .eslintrc format.
Exercise
A teammate says that ESLint alone is enough for a TypeScript project because the TypeScript compiler already checks types. Give two concrete reasons this is wrong, and name the part of typescript-eslint that addresses each reason.
Show answer
First, ESLint's default parser cannot read TypeScript syntax such as type annotations, interfaces and generics, so it reports parsing errors and runs no rules on those files. The typescript-eslint parser addresses this by producing an AST that includes TypeScript nodes. Second, the base ESLint rules assume every name is a runtime value and do not understand types, enums or overloads, so some produce false reports, and none can use type information. The typescript-eslint plugin addresses this with TypeScript-aware rules and replacements for problematic base rules, and its presets (with the optional type information from the compiler) add checks such as detecting unawaited Promises. The compiler is not a substitute for either, since it checks type correctness and does not flag risky patterns in code that compiles.
Exercise
You run npx eslint . in a fresh project and every .ts file fails with a parsing error at the first type annotation. Your eslint.config.mjs contains only export default defineConfig(eslint.configs.recommended);. What do you change?
Show answer
Import tseslint from typescript-eslint and add tseslint.configs.recommended to the config array after eslint.configs.recommended. That preset registers the TypeScript parser and plugin, so ESLint can parse type annotations. Make sure typescript-eslint and typescript are installed as dev dependencies.