DEV Community

Timo Ernst
Timo Ernst

Posted on • Originally published at timo-ernst.net

Prettier Autoformat for TypeScript not Working

For me Prettier has been doing an awesome job when auto-formatting JavaScript code on save. However, when using TypeScript files with React/JSX (.tsx) it didn’t work. Here is how to fix it:

First, press shift + cmd + p (Mac), enter “settings” and choose “Preferences: Open Settings (JSON)”. Then add the following:

"[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
"[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }
Enter fullscreen mode Exit fullscreen mode

Make sure you have set "editor.formatOnSave": true

That’s it! Happy coding! :-)

Oldest comments (19)

Collapse
 
shubhamverma profile image
Shubham Verma

Are you sure .tx is Typescript and not .ts ?

Collapse
 
timo_ernst profile image
Timo Ernst • Edited

Actually it's supposed to be .tsx :-) (fixed in post)
Good catch tho! Thanks for pointing out.

Collapse
 
prettydev profile image
Max

how to do on the windows10?

Collapse
 
timo_ernst profile image
Timo Ernst

Ctrl + Shift + P

Collapse
 
saroj8455 profile image
Saroj Padhan

OPen the vscode and press Ctrl + P

Collapse
 
goran7777 profile image
Goran

Thank you.

Collapse
 
saroj8455 profile image
Saroj Padhan

Thank you .. 😊 .

Collapse
 
oihamza profile image
Hamza

Wow, this was exactly what I was looking for!

Collapse
 
lakinduchandula profile image
lakinduchandula

It worked! thank you

Collapse
 
flashblaze profile image
Neeraj Lagwankar

Thank you so much!

Collapse
 
duyetvu2001 profile image
DuyetVu2001

Thank you so much!

Collapse
 
threadripper92 profile image
Threadripper92

Thanks for this.

Collapse
 
smboy86 profile image
smboy86

it works...
thanks