Set Up Prettier for TypeScript
Install Prettier to format TypeScript code automatically. Set up editor integration, CI checks, and make Prettier work alongside ESLint without conflicts.
Prettier is an opinionated code formatter. It reads your TypeScript files and rewrites them with consistent indentation, line width, quotes, trailing commas, and spacing. Unlike ESLint, which catches bugs and enforces code quality rules, Prettier only cares about how the code looks.
Formatting by hand wastes time in code review. Every comment about a missing semicolon or an extra space is a comment that could have been about logic. Prettier removes that entire category of feedback by enforcing one style automatically. For the complementary tool that catches bugs, see Set Up ESLint for TypeScript.
Install Prettier
Install Prettier with the exact-version flag so your entire team uses the same formatter:
npm install --save-dev --save-exact prettierThe --save-exact flag pins the version in package.json. Even a minor Prettier release can change formatting, so locking the version avoids surprises across different machines.
Create a Config File
Prettier works without configuration, but an empty config file tells editors that the project uses Prettier intentionally. Create one with this command:
echo "{}" > .prettierrcIf you want to customize Prettier's defaults beyond the empty config, here are the most useful options for TypeScript projects. Each one controls a specific formatting decision:
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100,
"tabWidth": 2
}The trailingComma option set to "all" adds commas after the last item in objects, arrays, and function parameters. This produces cleaner diffs when you add or remove items later. The printWidth option controls the maximum line length before Prettier wraps code.
Create a .prettierignore
Some files should never be formatted: build output, generated code, and coverage reports. Create a .prettierignore to exclude them:
dist
build
coverage
node_modules
package-lock.jsonPrettier also respects your .gitignore file, so files already ignored by git are automatically skipped. For a complete toolchain setup, see tsx vs ts-node for Running TypeScript to choose the right TypeScript runner for your scripts.
Format Your Project
Run Prettier across the entire project to format everything at once:
npx prettier . --writeFor a large project, you can target a specific directory instead. This is faster and limits the scope of changes in a single commit:
npx prettier src/ --writeMake Prettier Work with ESLint
Prettier and ESLint both touch your code, but they have different jobs. Prettier formats. ESLint checks for bugs. Some ESLint rules, however, also enforce formatting, and those rules will conflict with Prettier's decisions.
The solution is eslint-config-prettier. It turns off every ESLint rule that overlaps with Prettier's formatting domain:
npm install --save-dev eslint-config-prettierThen add it as the last entry in your ESLint config's extends array. It must come last so it overrides any formatting rules from earlier configs:
import js from "@eslint/js";
import tseslint from "typescript-eslint";
import prettier from "eslint-config-prettier";
export default tseslint.config({
files: ["**/*.{ts,js}"],
extends: [
js.configs.recommended,
tseslint.configs.recommended,
prettier,
],
});Now ESLint and Prettier will never disagree. ESLint catches the bugs. Prettier handles the formatting. The extends array order matters: prettier must be last so its rule overrides take priority.
Editor Integration
The best Prettier experience is format-on-save in your editor. In VS Code, install the Prettier extension and add this to your settings:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}With format-on-save enabled, you write code however you want, and Prettier cleans it up every time you save. You never think about indentation or line breaks again.
Run Prettier in CI
Like ESLint, Prettier is most effective when it blocks unformatted code from merging. Add a format check to your CI pipeline:
npx prettier . --checkThe --check flag reports which files are not formatted without modifying them. If any file is unformatted, the command exits with a non-zero code and the CI build fails.
Add convenience scripts to your package.json so you can format with a single npm command:
{
"scripts": {
"format": "prettier . --write",
"format:check": "prettier . --check"
}
}Now npm run format formats the entire project, and npm run format:check verifies formatting in CI without modifying files.
How Prettier Handles TypeScript
Prettier has built-in support for TypeScript. It understands type annotations, generics, as expressions, the satisfies keyword, and every TypeScript syntax node. You do not need a plugin.
Here is a typical TypeScript function before Prettier formats it. Notice the cramped spacing, missing type annotation gaps, and the long chained call all on one line:
function fetchUser(id:number):Promise<User|null>{
return fetch(`/api/users/${id}`).then(res=>res.json()).catch(err=>{console.error(err);return null})
}After running Prettier, the same function receives consistent spacing around type annotations, proper line breaks for the chain, and clean indentation in the catch block:
function fetchUser(id: number): Promise<User | null> {
return fetch(`/api/users/${id}`)
.then((res) => res.json())
.catch((err) => {
console.error(err);
return null;
});
}Prettier added consistent spacing around the type annotation, wrapped the long chain across lines, and formatted the catch block with proper indentation. The logic is identical. Only the presentation changed.
Rune AI
Key Insights
- Install Prettier as a dev dependency with --save-exact to lock the version.
- Create a .prettierrc config file and a .prettierignore to exclude generated files.
- Install eslint-config-prettier to turn off ESLint rules that conflict with Prettier.
- Enable format-on-save in your editor for the best experience.
- Run prettier --check in CI to block unformatted code from merging.
Frequently Asked Questions
Does Prettier replace ESLint for TypeScript?
Why install Prettier locally instead of globally?
How do I stop Prettier and ESLint from fighting?
Conclusion
Prettier takes formatting decisions out of code review. Install it locally, create a .prettierrc, add eslint-config-prettier so it works with ESLint, and run it in CI. Your team will never argue about semicolons again.
More in this topic
TypeScript Compiler Explained
The TypeScript compiler (tsc) turns .ts files into .js files while checking types. Learn what it does, how to use it, and how it fits your workflow.
Set Up ESLint for TypeScript
Install and configure ESLint with typescript-eslint to catch bugs and enforce consistent code style in your TypeScript project.
Pick Utility Type in TypeScript
Pick creates a new type by selecting only the properties you name from an existing type. Use it to build focused types for specific views, API responses, or function parameters.