在 VS Code 中保存时格式化 *.tsx 文件

Format *.tsx files on save in VS Code

settings.json 中,只能格式化 *.ts 文件:

"[typescript]": {
    "editor.formatOnSave": true
}

但我无法让它为 *.tsx 个文件工作。

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

另见 Language specific editor settings

使用像 'ESLint' 这样的插件的替代方法:

"[typescriptreact]": {
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}

您可以安装 Prettier Code formatter extension 并在 settings.json 文件中添加这两个选项

{
  "typescript.format.enable": false,
  "[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}