Sub topic 10
Keeping linting and formatting apart
This chapter assumes you know basic TypeScript, npm, and ESLint flat configuration. The goal is to give each tool one clear responsibility:
- ESLint checks code behavior, maintainability, and suspicious patterns.
- Prettier changes layout so code follows one consistent style.
Why the separation matters
Lint rules inspect code and report problems. Some rules understand TypeScript types or detect likely bugs. For example, ESLint can report an unused variable or a promise that is started without being handled.
Formatting rules care about appearance. They decide where to put line breaks, how to indent code, and whether to use single or double quotes. Prettier is designed to make these decisions consistently without asking developers to choose each time.
When both tools control formatting, they can disagree. A developer may run ESLint and then Prettier, only to have the files change again. The usual solution is to let Prettier own formatting and turn off ESLint rules that conflict with it.
Configure ESLint for code quality
Install the tools in a TypeScript project:
npm install --save-dev eslint typescript typescript-eslint @eslint/js prettier eslint-config-prettierThe eslint-config-prettier package disables ESLint rules that can conflict with Prettier. In a flat config, place its flat configuration after the other ESLint presets so its settings take effect.
// eslint.config.mjs
import eslint from '@eslint/js';
import eslintConfigPrettier from 'eslint-config-prettier/flat';
import { defineConfig } from 'eslint/config';
import tseslint from 'typescript-eslint';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
eslintConfigPrettier,
);The order matters. ESLint combines configuration entries in order. The later Prettier entry turns off conflicting stylistic rules after the recommended presets have enabled them.
Your Prettier settings can live in a separate file:
{
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}This division gives the tools a simple pipeline:
Run checks locally and in CI
Add separate npm scripts so developers can run either operation directly:
{
"scripts": {
"lint": "eslint .",
"format": "prettier . --write",
"format:check": "prettier . --check"
}
}Use npm run format when you want Prettier to rewrite files. Use npm run format:check in continuous integration (CI), where a nonzero exit code should reject incorrectly formatted code. Use npm run lint to find code-quality problems.
A common CI sequence is:
npm run format:check
npm run lintThe order is not required, but separate commands make failures clearer. An editor can run Prettier on save and display ESLint diagnostics while you type.
Avoid overlapping style rules
Do not enable a second set of formatting rules merely because they are available. Rules such as indentation, quote style, and line-break limits often duplicate Prettier's work. If a project needs a style preference that Prettier cannot express, document the exception and enable only that specific ESLint rule.
Exercise
A pull request fails because ESLint changes quote marks one way and Prettier changes them back. Which change should you make first?
Show answer
Let Prettier own quote formatting and disable the conflicting ESLint rule, usually by placing eslint-config-prettier after the other ESLint configurations.