Formatting
Prettier concept
Prettier is an opinionated formatter. You write code any way you like, it rewrites the file into one canonical layout. Debates about commas and quotes end because the tool decides.
The command below is shown as text only so you can recognize it in docs. No install step is shown here.
npx prettier .
A configured run prints the formatted code to standard output. With `--write` it rewrites the files and lists the ones it processed, and with `--check` it lists the files that need formatting.
Few options
Prettier works best with almost no configuration. Most teams set only quotes and line width, then accept every other default without discussion.
Fewer options mean fewer debates and faster reviews. Diffs stay small because only real logic changes, never whitespace noise.
{ "singleQuote": false, "printWidth": 100, "trailingComma": "all" }
Format on save
The real win is automation. Turn on format on save in the editor so every file is normalized the moment you save it.
After a week you stop thinking about layout entirely. Hands write logic, the tool handles alignment, and commits stay clean by default.
function fullName(first: string, last: string): string { return "Name: " + first + " " + last; } console.log(fullName("Ada", "Lovelace"));
Name: Ada Lovelace
End style fights
Code review should discuss design, not brace placement. Agree once that formatted output is always correct, then never comment on style again.
Newcomers onboard faster too. They run the formatter and instantly match house style with zero tribal knowledge.
const users: string[] = ["Ada", "Grace"]; const upper = users.map((u) => u.toUpperCase()); console.log(upper);
[ 'ADA', 'GRACE' ]
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does Prettier do?
Answer
It rewrites code into one canonical layout
The formatter owns the layout, so nobody has to argue about commas, quotes, or braces.
-
How much configuration does Prettier expect?
Answer
Almost none: a few choices such as quotes and line width
Fewer options mean fewer debates, and every other detail follows the tool default.
-
What is the real win of a formatter?
Answer
Automation, such as format on save, so layout is never manual
The editor formats each save, so diffs show logic changes and not whitespace.
-
What should a code review discuss once a formatter is in place?
Answer
Design and behavior, not brace placement
Formatted output is accepted as correct, which leaves the review for real decisions.
-
What does this program print?
function fullName(first: string, last: string): string { return "Name: " + first + " " + last; } console.log(fullName("Ada", "Lovelace"));Answer
Name: Ada Lovelace
The function joins the prefix and both names in order.
-
What does this program print?
const users: string[] = ["Ada", "Grace"]; const upper = users.map((u) => u.toUpperCase()); console.log(upper);Answer
[ 'ADA', 'GRACE' ]
The map makes a new array of upper case names, and the log prints it.
-
What does this program print?
console.log("a,b".split(",").length);Answer
2
The split makes two parts, a and b.
-
What does this program print?
console.log([3, 1, 2].sort().join(""));Answer
123
The default sort orders the numbers as text, and join removes the separators.
-
What does the compiler say about this program?
const users: string[] = ["Ada", 3]; console.log(users);Answer
error TS2322: Type 'number' is not assignable to type 'string'.
Every element of a string array must be a string, and 3 is a number.
-
What does the compiler say about this program?
const users: string[] = ["Ada", "Grace"]; console.log(users.map((u) => u.toFixed(2)));Answer
error TS2551: Property 'toFixed' does not exist on type 'string'. Did you mean 'fixed'?
A string has no toFixed method, and the compiler suggests the similarly named fixed.
Article author: Arthur Isaev