Sub topic 6
Applying different settings to different files
This chapter assumes you can already read an eslint.config.mjs file that uses defineConfig, and that you know what a preset such as recommended does. It builds on rule severity and options from the previous chapter. Here you will learn how one config file gives test files, scripts, and generated code different rules from your main source.
Why one set of rules is not enough
A single project usually contains several kinds of files:
- Application code in
src/, where you want strict rules. - Test files, where
anyand mocks are often practical. - Build scripts and plain JavaScript config files, which are not part of any TypeScript project.
- Generated code and build output, which you should not lint at all.
If you apply one rule set to all of them, you either weaken the rules everywhere or fight false alarms in the files that need different treatment. Flat config solves this with config objects that apply only to the files they name.
The flat config array is a list of layers
An eslint.config.mjs file exports an array of config objects. For each file ESLint lints, it picks the objects that match that file and merges them in order. A later object overrides an earlier one wherever they disagree.
Two keys control which files an object applies to:
| Key | Meaning |
|---|---|
| `files` | Glob patterns. The object applies only to matching files. |
| `ignores` | Glob patterns. In an object that also has `files`, they exclude files from that object. In an object with no other keys, they exclude files from linting entirely. |
An object with neither files nor ignores applies to every file ESLint lints. Patterns are resolved relative to the directory that contains the config file, not the directory you run the command from.
Global ignores
An object that contains only ignores is a global ignore. ESLint skips matching files and directories completely.
// eslint.config.mjs (first entry in the array)
{ ignores: ['dist/**', 'coverage/**', 'src/generated/**'] },Do not add any other key to this object. As soon as it has rules or files, the ignores patterns only narrow that one object, and the files are still linted by the others. ESLint also ignores node_modules and .git by default.
Scoping rules to file patterns
The most common override is a block with files and rules. This block relaxes two rules in tests and sets a core rule differently for source files and scripts:
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
{ ignores: ['dist/**', 'coverage/**'] },
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
name: 'project/type-aware-setup',
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
name: 'project/source',
files: ['src/**/*.ts'],
rules: {
'no-console': 'warn',
},
},
{
name: 'project/tests',
files: ['**/*.test.ts', 'tests/**/*.ts'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
},
},
{
name: 'project/scripts',
files: ['scripts/**/*.ts'],
rules: {
'no-console': 'off',
},
},
{
name: 'project/plain-js',
files: ['**/*.js', '**/*.mjs'],
extends: [tseslint.configs.disableTypeChecked],
},
);The name key is optional. It labels each object in error messages and in the config inspector, which helps once the array grows.
Three details in this file matter:
- Order decides conflicts. The preset turns
no-explicit-anyon as an error. Theproject/testsobject comes later and turns it off for test files. If you moved it above the preset, the preset would win and your override would do nothing. - Overrides merge rule by rule. An override only changes the rules it names. Every other rule from earlier objects stays in force for that file.
extendsworks inside an object. WithdefineConfig,extendsapplies the listed configs only to the files that object matches. HeredisableTypeCheckedturns off the type-checked rules for JavaScript files only.
How severity and options merge
When a later object sets a rule, ESLint combines it with the earlier setting:
- A later object that gives only a severity, such as
'warn', keeps the options from the earlier setting. - A later object that gives an array, such as
['warn', { ignoreVoid: false }], replaces the options with the new ones.
This lets you downgrade a rule for one folder without repeating its options:
// Earlier: strict options for all source files
{
files: ['src/**/*.ts'],
rules: {
'@typescript-eslint/no-floating-promises': ['error', { ignoreVoid: false }],
},
},
// Later: same options, lower severity for a legacy folder
{
files: ['src/legacy/**/*.ts'],
rules: {
'@typescript-eslint/no-floating-promises': 'warn',
},
},Files in src/legacy get a warning with ignoreVoid: false still applied.
Files that are not in your TypeScript project
Type-aware linting reads your tsconfig.json through the project service. A file that no tsconfig includes cannot be linted with type information. ESLint then reports a parsing error that says the file was not found by the project service.
You have three ways to fix this:
- Add the file to the
includelist of a tsconfig. Test files andscripts/are the usual cases. - Apply
disableTypeCheckedto the file, as theproject/plain-jsobject does above. You lose the type-checked rules for those files. - Add the file to the global
ignoresif it should not be linted at all.
Your eslint.config.mjs file is itself a .mjs file that no tsconfig includes, so the plain-js object also protects the config file from this error.
Matching files with other extensions
By default, ESLint lints only .js, .mjs, and .cjs files. A files pattern that ends in .ts tells ESLint to lint those files too. The typescript-eslint presets include such a pattern, which is why .ts files are linted once you add a preset. If you use .tsx, .mts, or .cts files, check that a pattern covers them. A rule override with files: ['**/*.ts'] does not apply to .tsx files.
A files entry can also be an array of patterns inside an array. Those inner patterns must all match, which works as AND:
{
// Only test files inside src/api
files: [['src/api/**/*.ts', '**/*.test.ts']],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
},Checking what ESLint actually applies
When an override seems to be ignored, ask ESLint for the final config of one file instead of guessing:
npx eslint --print-config src/user.test.ts
npx eslint --inspect-configThe first command prints the merged settings for that file as JSON. Look for the rule you changed and its severity. The second opens a browser-based config inspector that shows which objects match each file. If the rule shows the wrong value, the usual causes are:
- The override sits above the preset in the array.
- The
filespattern does not match, for example because of a wrong folder or extension. - The pattern is relative to a different directory than you expected.
Common mistakes
- Putting a preset after your overrides, so the preset silently undoes them.
- Using
files: ['*.test.ts'], which matches only files in the config's own directory. Use**/*.test.tsto match at any depth. - Disabling a rule for the whole project when only one folder needs it.
- Forgetting that
extendsinside an object limits the extended configs to that object'sfiles.
Exercise
Your project lints with recommendedTypeChecked. The scripts/ folder holds .ts files that are not in any tsconfig, and ESLint reports "file was not found by the project service" for them. You also want no-console turned off there. Write the config object that fixes both problems, and say where in the array it goes.
Show answer
Add this object after the presets and the project service setup:
{
files: ['scripts/*/.ts'],
extends: [tseslint.configs.disableTypeChecked],
rules: { 'no-console': 'off' },
},
disableTypeChecked turns off the rules that need type information and the project requirement for those files, so the error goes away. It must come after the preset, because later objects win. The alternative is to add scripts to a tsconfig include, which keeps type-checked rules active there.
Exercise
A config has { ignores: ['dist/**'], rules: { 'no-console': 'off' } }, and ESLint still lints files in dist. Why, and how do you fix it?
Show answer
Because the object has a rules key, the ignores pattern only excludes dist files from that one object. Other objects still match them. Split it into two objects: { ignores: ['dist/**'] } as a global ignore, and a separate object for the no-console rule with the files pattern you need.