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.
Update 2024
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.
.vscode
folder with a settings.json
file every time? –
Codycoe Ctrl +,
then search the typescript –
Corvese Edit setting with following in settings.json
of VScode
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
typescriptreact
when I already had "editor.defaultFormatter": "esbenp.prettier-vscode"
set at both a global and workspace level. –
Aricaarick Open User Settings (JSON)
–
Toothwort 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" }
Update 2024
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.
.vscode
folder with a settings.json
file every time? –
Codycoe Ctrl +,
then search the typescript –
Corvese 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.
TypeScript > Format: Enable
it started working for my .tsx files as well. –
Polycotyledon 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
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.
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.
TypeScript > Format: Enable
. –
Polycotyledon After setting Prettier as the default formatter in Vs-code and checking 'format on save' it still was not working for me. What finally did it was deleting and then recreating my project's .prettierrc
file.
"[javascriptreact,typescript,typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
A settings.json
like the below would work. typescriptreact
is so-named because .jsx/.tsx
are JavaScript/TypeScript syntax extensions introduced by React.
{
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
}
If the above two lines added to your settings.json
did not resolve the issue, you can go to Menu
-> Preferences
-> Settings
and search for prettier require config
and disable it, like so:
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 :)
© 2022 - 2024 — McMap. All rights reserved.
prettier
as a dependency of this project? – Arbitration