Sub topic 6
Applying Different Settings to Different Files
Why file-scoped settings matter
This chapter assumes that you know basic TypeScript syntax, have a tsconfig.json file, and can use npm, Node.js, and ES module imports.
A project rarely contains only one kind of source file. Application code may use strict TypeScript rules, test files may need test globals, and configuration scripts may be plain JavaScript. ESLint lets you apply different settings to each group of files.
In flat config, an ESLint configuration is an array of configuration objects. A files property selects the files that an object affects. If several objects match one file, ESLint combines them. Later objects can override earlier settings.
Use `files` to select a group
A common pattern starts with a base configuration and adds rules for 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',
'@typescript-eslint/consistent-type-imports': 'warn',
},
},
);The first two entries provide general and TypeScript rules. The third entry changes rule severity only for .ts and .tsx files.
The most useful file patterns include:
| Pattern | Files selected |
|---|---|
| `**/*.ts` | TypeScript files in any directory |
| `**/*.tsx` | TypeScript files containing JSX |
| `**/*.test.ts` | TypeScript test files with this suffix |
| `scripts/**/*.js` | JavaScript files under `scripts` |
A configuration object without files usually applies to every linted file. Add files when a setting should not affect the entire project.
Override rules for JavaScript files
Type-aware rules need a TypeScript project. JavaScript configuration files often are not included in tsconfig.json, so applying type-checked rules to them can produce parser errors.
typescript-eslint provides disableTypeChecked for this situation:
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ['**/*.js', '**/*.mjs', '**/*.cjs'],
extends: [tseslint.configs.disableTypeChecked],
},
);The final object matches JavaScript files and turns off rules that require type information. It does not disable ordinary ESLint rules.
Common mistakes
- Using the wrong glob:
*.tsgenerally matches files in one directory, while**/*.tsmatches nested directories too. - Forgetting test files: Test code may need separate globals or relaxed rules. Add a
filesentry for patterns such as**/*.test.ts. - Applying type-aware rules to scripts: Exclude JavaScript tools or disable type-checked rules for them.
- Assuming every setting merges the same way: Rule settings merge by rule name, but some options such as parser settings can replace earlier values.
- Adding too many exceptions: Overrides should describe real file categories. A long list of one-off exceptions can hide inconsistent code.
You can inspect the final configuration for a file with:
npx eslint --print-config src/app.ts
npx eslint --print-config scripts/build.mjsCompare the output to confirm which rules apply to each file.
Exercise
Exercise
You use recommended type-checked rules for src/**/*.ts, but tools/release.mjs is not part of the TypeScript project. What kind of override should you add?
Show answer
Add a later configuration object with files: ['tools/**/*.mjs'] and extend tseslint.configs.disableTypeChecked so type-aware rules do not require project information for that script.