除非提供'--jsx'标志,否则不能使用JSX。

17 浏览
0 Comments

除非提供'--jsx'标志,否则不能使用JSX。

我已经找了一些解决这个问题的方案。它们都建议在你的tsconfig.json文件中添加\"jsx\": \"react\"。我已经做了。还有一种方法是添加\"include: []\",我也做了。然而,当我尝试编辑.tsx文件时,仍然会出现错误。下面是我的tsconfig文件。

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "allowJs": true,
        "checkJs": false,
        "jsx": "react",
        "outDir": "./build",
        "rootDir": "./lib",
        "removeComments": true,
        "noEmit": true,
        "pretty": true,
        "skipLibCheck": true,
        "strict": true,
        "moduleResolution": "node",
        "esModuleInterop": true
    },
    "include": [
        "./lib/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

如果您需要更多的文件来帮助调试这个问题,请让我知道。我正在使用babel 7来编译所有带有env、react和typescript预设的代码。

admin 更改状态以发布 2023年5月20日
0
0 Comments

如果没有提供'--jsx'标志,就无法使用JSX

重新启动你的IDE。有时候tsconfig.json的更改不会立即生效。

0
0 Comments

每次运行`npm start`时,它都会用 `react-jsx` 覆盖我在`{jsx:...}`中配置的任何配置,以便与React 17中的JSX变换兼容。

The following changes are being made to your tsconfig.json file:
  - compilerOptions.jsx must be react-jsx (to support the new JSX transform in React 17)


问题在于VSCode使用了较旧版本的typescript(4.0.3),而项目中附带的typescript版本是(4.1.2)。
以下是我用来解决问题的步骤:

  1. 打开命令面板 CTRL + Shift + P (或 Mac 上的 + Shift + P )。
  2. 选择 "TypeScript: Select a TypeScript Version..."。
  3. 选择 "Use workspace Version"。

VSCode status bar

VSCode command palette

TypeScript workspace version

PS:除非您在任何 `.tsx` 文件上,否则不会显示此选项(感谢@awran5的评论并捕捉)。

0