Sub topic 2
Setting up ESLint with a flat config file
This chapter assumes you can write TypeScript and read a tsconfig.json. It also assumes you can run npm scripts and know ES module import and export syntax. By the end you will have a working eslint.config.mjs that lints a TypeScript project. You will also know how ESLint reads that file and how to diagnose the usual setup errors.
The working model: a config is an array of objects
ESLint 9 and later read a configuration format called flat config. A flat config file exports an array of plain objects. Each object is one configuration entry. For every file ESLint lints, it walks the array from top to bottom. It collects the entries that apply to that file and merges them. When two entries set the same option, the later one wins.
An entry can contain these keys:
files: glob patterns that limit the entry to matching files. Withoutfiles, the entry applies to every file that ESLint lints.ignores: glob patterns of files the entry does not apply to.languageOptions: the parser and how the code is interpreted, for example the module system.plugins: named plugin objects whose rules you can then reference.rules: rule names mapped to a severity ("off","warn"or"error") and optional options.linterOptions: settings about linting itself, such as reporting unused disable directives.
This model replaces the older .eslintrc system. That system used cascading files in directories, an extends string list and overrides blocks. A flat config has one file and one ordered array, so you can always answer "why is this rule on?" by reading the array in order.
Installing the packages
Install ESLint, its recommended rule set and the typescript-eslint package:
npm install --save-dev eslint @eslint/js typescript-eslinttypescript-eslint is a single package that bundles the parser, the plugin and the preset configs. You do not install @typescript-eslint/parser or @typescript-eslint/eslint-plugin separately. TypeScript itself must also be installed in the project, because the parser uses it.
Creating the config file
ESLint looks for eslint.config.js, eslint.config.mjs or eslint.config.cjs in the directory where you run it. Newer versions also accept eslint.config.ts, but that needs extra runtime support, so this tutorial uses .mjs. The .mjs extension guarantees ES module syntax even if your package.json has no "type": "module" field.
Create eslint.config.mjs in the project root:
// 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,
);This config has two parts:
eslint.configs.recommendedturns on ESLint's core rules that catch likely mistakes, such asno-unused-varsandno-undef.tseslint.configs.recommendedis an array of entries from typescript-eslint. It registers the TypeScript parser and the plugin, adjusts core rules that misbehave on TypeScript code, and enables the plugin's recommended rules.
defineConfig is a helper from ESLint. It flattens nested arrays, which is why you can pass a preset that is itself an array. It also gives editors type information while you write the config. It is available in ESLint 9.22 and later. If your version is older, use tseslint.config(...) with the same arguments.
Running ESLint
Run ESLint from the project root. The first command reports problems and the second also applies automatic fixes:
npx eslint .
npx eslint . --fixRules that can fix themselves, such as prefer-const, are rewritten in place. Other problems are only reported and you must fix them by hand. Add a script to package.json so that your team and your CI system use the same command:
{
"scripts": {
"lint": "eslint ."
}
}Which files get linted
ESLint lints a file only if some config object has a files pattern that matches it. By default, patterns cover .js, .cjs and .mjs files. The typescript-eslint presets add entries whose files patterns cover .ts, .tsx, .mts and .cts. That is why the two-line config above is enough to lint TypeScript. If you write your own entry for TypeScript files with a custom extension, you must name that extension in files.
Two other defaults matter:
- ESLint ignores
node_modulesand.gitautomatically. - ESLint also lints the config file itself, because
eslint.config.mjsmatches the default patterns.
Ignoring files with global ignores
Build output such as dist and generated code usually should not be linted. Add an entry that contains only ignores:
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
{ ignores: ['dist/**', 'coverage/**', '**/*.generated.ts'] },
eslint.configs.recommended,
tseslint.configs.recommended,
);The position of this entry in the array does not matter. The shape does: an object with only an ignores key is a global ignore, and ESLint skips those files entirely. If the object also has files, rules or any other key, the patterns only exclude files from that one entry. Files in dist would then still be linted by the other entries.
Adding your own entry
The presets are a starting point. To change a rule, add an object after them, so that it overrides what the presets set:
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
{ ignores: ['dist/**'] },
eslint.configs.recommended,
tseslint.configs.recommended,
{
rules: {
'no-console': 'warn',
},
},
);Placing that last entry before the presets would be a mistake. The preset would then run later, and if it set no-console itself, it would override your choice. The order of the array is the main thing to check when a setting seems to be ignored. Later chapters cover rule tuning and file-scoped entries in more detail.
Common errors and what they mean
| Symptom | Likely cause | Fix |
|---|---|---|
| `Could not find config file` | No `eslint.config.*` in the directory where you ran ESLint | Run from the project root, or create the file |
| `Parsing error: Unexpected token` on type annotations | The TypeScript parser is not registered for that file | Include `tseslint.configs.recommended`, and check that `files` covers the extension |
| `Cannot use import statement outside a module` | The config file uses `import` but is loaded as CommonJS | Rename it to `eslint.config.mjs`, or set `"type": "module"` |
| Rules from an `.eslintrc` file have no effect | ESLint 9 does not read `.eslintrc` files by default | Move the settings into the flat config |
| `dist` files still reported | The ignore entry has other keys, so it is not global | Put `ignores` alone in its own object |
| `no-undef` reports `process` or `describe` | The environment globals are not declared | Add `languageOptions.globals`, for example from the `globals` package |
Checking your setup
Create src/demo.ts with this content:
let count = 1;
const unused = 'never read';
export function total(): number {
return count + 1;
}Run npx eslint .. You should see a report for unused from @typescript-eslint/no-unused-vars. If you see a parsing error instead, return to the table above. After a clean setup, npx eslint . --fix changes let count to const count, because prefer-const has a fix. It leaves the unused variable alone, because deleting code is not a safe automatic fix.
Exercise
You add { ignores: ['dist/**'], rules: { 'no-console': 'off' } } to your config, and ESLint still reports problems in dist/app.js. Why, and how do you fix it?
Show answer
An object that has keys besides ignores is not a global ignore. Its ignores pattern only removes dist files from that one entry, so other entries still lint them. Split it into two objects: { ignores: ['dist/**'] } on its own, and a separate { rules: { 'no-console': 'off' } }.
Exercise
You place { rules: { 'no-console': 'warn' } } before tseslint.configs.recommended in the array, and a rule you set there seems to have no effect. What should you check first?
Show answer
Check the order of the array. Later entries override earlier ones, so a preset placed after your entry can override your setting if the preset sets that rule. Move your custom entry after the presets.