Sub topic 2
Setting up ESLint with a flat config file
This chapter assumes you can read TypeScript, have a tsconfig.json, and know basic npm, Node.js, and ES module syntax. You will create an ESLint flat config that checks TypeScript files with the recommended rules.
Install the packages
ESLint provides the linting engine. The typescript-eslint package connects ESLint to TypeScript by providing a parser and rules designed for TypeScript code. The parser converts TypeScript source into a form ESLint can analyze.
Install the packages as development dependencies:
npm install --save-dev eslint @eslint/js typescript typescript-eslintYour project should already contain a tsconfig.json. ESLint can lint many files without reading that project, but later chapters will use the project for type-aware rules.
Create the flat config
Flat config uses an ordinary JavaScript or TypeScript module instead of a hierarchy of .eslintrc files. 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,
);The exported value is a list of configuration objects. ESLint combines these objects to decide which files to inspect and which rules to apply.
The two presets have different responsibilities:
| Configuration | Purpose |
|---|---|
| `eslint.configs.recommended` | General JavaScript rules, such as detecting unused variables |
| `tseslint.configs.recommended` | TypeScript-aware rules that work without TypeScript type information |
The typescript-eslint recommended preset also supplies the TypeScript parser and plugin. A parser reads source code into a structure that ESLint can inspect. A plugin adds rules and configuration presets to ESLint.
Run the linter
Add a script to package.json:
{
"scripts": {
"lint": "eslint ."
}
}Run it from the project root:
npm run lintThe . tells ESLint to inspect files below the current directory. ESLint automatically finds eslint.config.mjs. If a rule reports a problem, the output includes the file, line, column, rule name, and message.
You can also run ESLint directly:
npx eslint srcThis checks only the src directory. Keep the command in package.json so editors, CI systems, and other developers use the same entry point.
Understand configuration matching
A flat config is evaluated as a sequence. A configuration object can apply globally or only to files matched by its files property. This lets one file control the project without copying settings across directories.
For example, the following configuration adds a stricter rule only to TypeScript files:
// 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,
{
files: ['**/*.ts', '**/*.tsx'],
rules: {
'@typescript-eslint/no-explicit-any': 'error',
},
},
);The glob **/*.ts matches TypeScript files in nested directories. The **/*.tsx pattern does the same for TypeScript files containing JSX. Rules in a later matching object can add to or override earlier settings.
Check the setup
Create a small error in a TypeScript file:
const unusedValue: number = 42;Run npm run lint. The recommended rules should report that unusedValue is never used. Remove the declaration or use the value, then run the command again.
Exercise
Which package connects ESLint to TypeScript, and which file does ESLint load for this setup?
Show answer
The typescript-eslint package connects ESLint to TypeScript, and ESLint loads eslint.config.mjs from the project root.