36

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
David
  • 4,191
  • 2
  • 31
  • 40

6 Answers6

64
"[typescriptreact]": {
    "editor.formatOnSave": true
}

See also Language specific editor settings

David
  • 4,191
  • 2
  • 31
  • 40
  • 4
    This doesn't work as of Oct 15 2020, any ideas what has happened? – James Oct 15 '20 at 17:53
  • Hmm I just checked and it seems to be working for me. Are you missing an update? Do you have it set to false somewhere else? Do you have the formatter disabled? – David Oct 15 '20 at 18:01
  • 8
    Finally got it to work, I needed to run the formatted manually first before it would do it on save. There were conflicting linters trying to lint the same file but it threw no errors. The above snippet needs the explicit setting of which linter you want it to use as well if you have multiple installed. – James Oct 16 '20 at 11:43
  • 3
    Yes, I also faced same problem. To setup default linter open the command palette Ctrl+Shift+P and configure the default formatter. – Jaskaran Singh Feb 08 '21 at 13:47
  • To follow-up on @JaskaranSingh, Ctrl-Shift-P, start typing and then select "Format Document", and it'll ask for a default formatter. After that, the "formatOnSave" setting will pick up. – ruffin Jun 26 '23 at 21:04
14

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"
  }
}
Safwat Fathi
  • 758
  • 9
  • 16
7

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
},
Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62
6

Alternative way with using a plugin like 'ESLint':

"[typescriptreact]": {
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}
TheRemoteCoder
  • 172
  • 2
  • 12
2
"[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
    }
  },
Wilfred
  • 69
  • 9
0

In my case, I messed up with keyboard shortcuts.

  • Problem faced: Format on save was not working.
  • Solution: I opened my keyboard shortcut json file and searched for cmd+s and found ther was another shortcut that I created for split right that contained cmd+s cmd+r.
  • Summary: Once I removed keyboard shortcuts started from cmd+s from all custom shortcuts that I created. It worked.
Rohit Kumar
  • 983
  • 9
  • 11