I've used the Prettier extension in the visual studio code editor for a long time, but recently I have been writing to React with Typescript. So I need to configure for Prettier to format .tsx
files.

- 110,530
- 99
- 389
- 494

- 5,266
- 3
- 37
- 59
-
Do you have `prettier` as a dependency of this project? – Emile Bergeron May 11 '20 at 14:17
-
I just had to restart the VS Code. – keremistan Nov 29 '22 at 14:35
9 Answers
Edit setting with following in settings.json
of VScode
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},

- 5,266
- 3
- 37
- 59

- 2,283
- 2
- 12
- 10
-
8In case you are wondering how to access settings.json -> https://stackoverflow.com/a/65909052/5888998 – Benjamin Castor Oct 14 '21 at 12:56
-
5To open settings follow these steps: Shift + CMD + P, Type "settings", Click on "preferences: open settings(JSON)" – Uche Azinge Oct 17 '21 at 08:56
-
8This worked - thank you - but I don't understand why I had to specifically add the block for `typescriptreact` when I already had `"editor.defaultFormatter": "esbenp.prettier-vscode"` set at both a global and workspace level. – Axesilo Jul 30 '22 at 18:39
Expanding on iNeelPatel's answer, I had to add two lines to VSCode JSON settings:
"[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
"[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }

- 993
- 8
- 11
Update 2023
Create a .vscode
folder at the root of your project. In the .vscode
folder, create the settings.json
file, and in it, write this section:
{
"[typescript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Don't forget to add .vscode
in the .gitignore
file.

- 5,266
- 3
- 37
- 59
-
This works; however, how can we apply this to all projects? Do we have to create a `.vscode` folder with a `settings.json` file every time? – Anthony Avila May 30 '23 at 03:32
-
@AnthonyAvila You can modify VSCode settings globally by pressing `Ctrl +, ` then search the typescript – S. Hesam May 30 '23 at 09:07
An easy UI alternative to what has been proposed already:
- Search for "default formatter" in your vscode settings.
- Click on "Text Editor" and set the default formatter to "Prettier - Code formatter".
- Enjoy.

- 275
- 3
- 6
-
For some reason this was working for me for .js files only. But then after I also deactivated `TypeScript > Format: Enable` it started working for my .tsx files as well. – Woodchuck Mar 16 '23 at 19:51
This will give perfect solution
"Same thing happened to me just now. I set prettier as the Default Formatter in Settings and it started working again. My Default Formatter was null."
https://github.com/microsoft/vscode/issues/108447#issuecomment-706642248

- 168
- 1
- 2
- 8
My Usage
The way I set this up is to use eslint's .eslintrc.json
file. First of all, in the "extends"
array, I've added
"plugin:@typescript-eslint/recommended"
and
"prettier/@typescript-eslint"
Then, I've set "parser"
to "prettier/@typescript-eslint"
Finally, in "plugins"
array I've added "@typescript-eslint"
.
You'll need to grab a couple of NPM packages (install with the -D
option):
@typescript-eslint/eslint-plugin
@typescript-eslint/parser
For reference, my entire .eslintrc.json
file:
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"project": "./src/tsconfig.json",
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint", "jest"],
"rules": {
"react/react-in-jsx-scope": "off"
}
}
Hope this helps.

- 1,337
- 8
- 20
On my side simply adding the two lines in the config file didn't work but deactivating the Typescript > Format: Enable
option in settings worked.

- 99
- 1
- 7
-
This fixed this issue for me! Not sure why, as it's counterintuitive. I'm using the VSCode prettier extension and have my User [Javascript] Default Formatter pointing to that (Prettier). That was working for .js files. But it was not working for .tsx files - until I deactived `TypeScript > Format: Enable`. – Woodchuck Mar 16 '23 at 19:50
"[javascriptreact,typescript,typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},

- 21
- 2
Perhaps helpful to someone - if with all the above, still no luck, you may want to restart VSCode or from command palette (⌘CMD + ⇧Shift + P
) and fire up "Restart ESLint Server" - that shd do, then :)

- 599
- 6
- 14