In settings.json
it is possible to only format *.ts
files with:
"[typescript]": {
"editor.formatOnSave": true
}
but I can't get it to work for *.tsx
files.
In settings.json
it is possible to only format *.ts
files with:
"[typescript]": {
"editor.formatOnSave": true
}
but I can't get it to work for *.tsx
files.
"[typescriptreact]": {
"editor.formatOnSave": true
}
See also Language specific editor settings
You can install Prettier Code formatter extension and add these two options in your settings.json file
{
"typescript.format.enable": false,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
In my case, I added both in my settings.json
as I noticed the .ts file is not formatted when I save, here's the updated settings:
"[typescript]": { // for .ts files
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescriptreact]": { // for .tsx files
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"editor.codeActionsOnSave": { // apply ESLint
"source.fixAll.eslint": true
},
Alternative way with using a plugin like 'ESLint':
"[typescriptreact]": {
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode", /// TSX auto format on save Prettier
"editor.formatOnSave": true,
"editor.codeActionsOnSave": { /// TSX auto format on save ESLint
"source.fixAll.eslint": true
}
},
In my case, I messed up with keyboard shortcuts.
cmd+s
and found ther was another shortcut that I created for split right that contained cmd+s cmd+r
.cmd+s
from all custom shortcuts that I created. It worked.