Sub topic 10
Keeping linting and formatting apart
This chapter assumes you can already write an eslint.config.mjs with the typescript-eslint recommended preset, as in the earlier chapters. It also assumes you know what a rule severity is. Here you will learn why linting and formatting are different jobs, how to give each job to its own tool, and what to do when the two tools disagree.
Two jobs that look alike
A linter and a formatter both read your source code and both can change it. They answer different questions.
- A linter asks whether the code is probably wrong or risky. It looks at meaning: an unused variable, a promise nobody awaits, an
anythat hides a type error. - A formatter asks how the code should look. It looks at layout: line width, quote style, trailing commas, indentation.
Compare these two snippets. Both have a problem, but the problems are different kinds.
// Formatting problem only: the program is correct, the layout is messy.
const user = {name:'Ada',
role: 'admin'}
// Linting problem: the layout is fine, the behavior is likely a bug.
async function save(id: string): Promise<void> {
// persist the record
}
export function run(): void {
save('a');
}No formatter can tell you that save('a') drops a promise. No linter should spend its time deciding whether a line break comes before or after an operator. A formatter is deterministic: given the same input and options, it always prints the same output, and it ignores how you wrote the code before.
What typescript-eslint does and does not cover
typescript-eslint rules fall into two groups.
- Correctness and bug-finding rules, such as
@typescript-eslint/no-floating-promisesand@typescript-eslint/no-unused-vars. These belong in ESLint. - Style-of-code rules, such as
@typescript-eslint/consistent-type-definitions(preferinterfaceortype) and@typescript-eslint/array-type. These are about which language feature you pick, not about whitespace.
The stylistic preset from the earlier chapter contains the second group. The word "stylistic" can mislead. It does not format your code. It enforces consistent choices between equivalent syntax. A formatter never rewrites Array<string> into string[], because that changes the syntax tree, and formatters only change layout.
Older versions of typescript-eslint also shipped pure formatting rules, such as indent, semi, and member-delimiter-style. These were deprecated and then removed from the core plugin. If you find them in an old tutorial or an inherited config, replace them with a formatter. ESLint's own core formatting rules (indent, quotes, comma-dangle, and so on) are also deprecated.
Why separate them
Putting layout rules into ESLint causes real problems.
- Speed. A formatter works from tokens and a printer algorithm. Layout rules in ESLint run through the rule engine and each report is a lint message, so large files take longer.
- Noise. A missing comma or wrong quote becomes a lint error in the editor and in CI, and it buries the reports that matter.
- Conflicts. If ESLint says "use single quotes" and Prettier prints double quotes,
--fixand "format on save" undo each other. - Fix loops. Layout fixes need several passes because one fix moves the code that the next fix examines.
The common arrangement is this: Prettier owns layout, ESLint owns correctness and code-style choices, and ESLint is told to stay silent about anything Prettier controls.
Setting up Prettier next to ESLint
Install Prettier and the config package that switches off conflicting ESLint rules.
npm install --save-dev prettier eslint-config-prettiereslint-config-prettier is a flat-config-compatible array of rule overrides that sets every rule that could clash with Prettier to "off". It contains no rules of its own. Because it only turns rules off, its position in the config array matters: it must come after every entry that could turn those rules back on.
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintConfigPrettier from 'eslint-config-prettier';
import { defineConfig } from 'eslint/config';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
tseslint.configs.stylistic,
{
files: ['**/*.ts'],
rules: {
'@typescript-eslint/consistent-type-imports': 'warn',
},
},
eslintConfigPrettier,
);Prettier has its own, small config file. Keep it short, because Prettier is deliberately opinionated.
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100
}Save that as .prettierrc. Then add scripts so each tool has its own command.
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}Prettier reads .prettierignore (same syntax as .gitignore) to skip build output. ESLint's flat config uses a global ignores entry for the same purpose. Keep the two lists in sync, or ESLint may lint generated files that Prettier skips.
What the config package disables
A rule is switched off when its output could differ from Prettier's output. The table shows examples from the two groups you meet in a TypeScript project.
| Rule | Area | Effect of `eslint-config-prettier` |
|---|---|---|
| `indent`, `quotes`, `semi`, `comma-dangle` | Core ESLint layout | Turned off |
| `max-len`, `no-mixed-operators` | Core ESLint, layout adjacent | Turned off (special cases, see below) |
| `@typescript-eslint/no-unused-vars` | Correctness | Not touched, still runs |
| `@typescript-eslint/array-type` | Syntax choice | Not touched, still runs |
| `@typescript-eslint/no-floating-promises` | Correctness, type-checked | Not touched, still runs |
A few rules are listed as "special": curly, max-len, no-unexpected-multiline, and quotes among them. They can be used with Prettier only under specific options, and the config package turns them off by default. If you enable one on purpose, follow the notes in the package's README, since a wrong option combination brings back the conflict.
The plugin alternative, and why it is not the default
eslint-plugin-prettier runs Prettier as an ESLint rule, so formatting differences appear as lint errors and eslint --fix formats your files. This gives you one command. It also has costs: lint output fills with "Insert ;" style messages, editors show red squiggles for whitespace, and each file is formatted inside the lint pass. Prettier's own documentation recommends running Prettier as a separate step instead. Use the plugin only if your team needs a single command and accepts those costs.
Order of operations
When both tools can change files, the order affects the result.
- Run ESLint with
--fixfirst. Its fixes, such as rewritingArray<string>tostring[], change code structure and may leave odd layout behind. - Run Prettier with
--writesecond. It reprints the final text and normalizes layout.
Running them in the other order can leave a file that fails prettier --check after an ESLint fix. A simple script handles this:
npx eslint . --fix
npx prettier --write .In CI, run only the checking forms, and never let CI write to files.
npx eslint .
npx prettier --check .Checking for leftover conflicts
If you suspect a rule still fights Prettier, ESLint can show you the final config for a file.
npx eslint --print-config src/index.tsSearch the output for the rule name. If it shows a severity of 2 or 1 and the rule is about layout, something later in your array re-enabled it. The usual cause is a preset or shared config placed after eslintConfigPrettier. Move eslintConfigPrettier to the end of the array.
Common errors
- Format on save and
--fixfight each other. A layout rule is still active. Check array order, then use--print-config. eslint-config-prettierhas no effect. It was placed before the rule-enabling entries. Order in a flat config is significant, and later entries win.- Prettier changes files ESLint ignores, or the reverse. The ignore lists differ. Align
.prettierignoreand the flat configignores. - Prettier does not format your config file.
.mjsand.jsfiles are formatted by default, but only if they are not ignored. Check.prettierignore. - Old
@typescript-eslint/indentorsemierrors appear. Your config references removed rules. Delete those entries and rely on Prettier.
Exercise
A teammate adds '@typescript-eslint/array-type': ['error', { default: 'array' }] to the ESLint config and asks whether eslint-config-prettier will switch it off. What is the answer, and why?
Show answer
No. eslint-config-prettier only disables rules whose output could conflict with Prettier's layout output. array-type chooses between string[] and Array<string>, which changes syntax rather than layout, and Prettier does not rewrite either form. The rule keeps running, and eslint --fix can apply its fix.
Exercise
After adding eslintConfigPrettier as the second entry of your flat config array, npx eslint . still reports a quotes error from a later entry that enables the rule. What is the fix, and what command can confirm it worked?
Show answer
Move eslintConfigPrettier to the last position in the array so it overrides every earlier entry that enables layout rules. Confirm with npx eslint --print-config src/index.ts and check that quotes is 0 or "off" in the printed config.