Sub topic 1
Linting TypeScript Code with typescript-eslint
Prerequisites
You should know basic TypeScript syntax, npm, Node.js commands, ES module imports, and the purpose of a tsconfig.json file.
What linting does and why TypeScript needs typescript-eslint
Linting analyzes source code and reports patterns that may indicate bugs, confusing code, or inconsistent practices. ESLint is the JavaScript linting engine. It runs rules against a parsed syntax tree before code reaches production.
TypeScript adds syntax and semantics that standard ESLint does not fully understand. typescript-eslint supplies a TypeScript parser and ESLint rules for TypeScript. Its rules can check both syntax and, when configured, type information.
Setting up ESLint with a flat config file
Modern ESLint uses a flat configuration file, usually eslint.config.mjs. Install ESLint, @eslint/js, and typescript-eslint, then create this minimal configuration:
// 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 exported array combines ESLint's JavaScript recommendations with typescript-eslint recommendations. Run it with:
npx eslint .How the parser and plugin fit together
The parser converts TypeScript syntax into the structure ESLint reads. The plugin contributes TypeScript-specific rules and presets. The recommended preset enables a practical baseline without requiring type information.
This separation matters: installing the plugin does not automatically enable every rule. A preset or explicit rules entry must select the rules.
Choosing a preset
Presets represent different levels of enforcement:
| Preset | Best use | Trade-off |
|---|---|---|
| `recommended` | Most projects | Smaller rule set |
| `strict` | Teams wanting stronger checks | More fixes and decisions |
| `stylistic` | Consistent code style | Can overlap with formatters |
Each has type-checked variants. Start with recommended; adopt stricter presets after reviewing reports. Keep formatting rules in a formatter such as Prettier rather than making ESLint enforce every whitespace choice.
Exercise
What does typescript-eslint add to ESLint?
Show answer
It adds a parser for TypeScript syntax and rules that understand TypeScript, with optional rules that use type information.