在 TypeScript 中使用 jsx 的文件必须具有扩展名 tsx 吗?

Must files using jsx in TypeScript have the extension tsx?

似乎 TypeScript 和 tslint 不接受扩展名为 ts 的文件中的 jsx 代码。在 TypeScript 中使用 jsx 的源文件是否必须使用扩展名 tsx?

是的,必须将 jsx 放入 tsx 文件中。 docs

中对此进行了解释

In order to use JSX you must do two things.

  1. Name your files with a .tsx extension
  2. Enable the jsx option

这样做既是为了模仿 jsx 传统上使用的扩展,但可能更重要的是因为 Typescript 与 jsx 有一些语法冲突(例如旧的类型断言运算符 <type>),所以使用单独的扩展有助于向后兼容。