使用“react-jsx”作为带有 create-react-app 的 jsx 值的 Visual Studio Code 问题

Den*_*bon 73 jsx typescript tsconfig visual-studio-code create-react-app

我正在努力解决 VSCode 中的“错误”:

Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'
Run Code Online (Sandbox Code Playgroud)

因此,当value 似乎有效时,react-scripts (create-react-app) 会自动将jsxkey设置为react-jsxvalue react

实际上,代码运行良好并显示了我想要的页面,但是 IDE 将所有内容都强调为错误,并说:

Cannot use JSX unless the '--jsx' flag is provided.
Run Code Online (Sandbox Code Playgroud)

这是我的 tsconfig.json :

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

Run Code Online (Sandbox Code Playgroud)

,和我的 package.json (默认由 create-react-app 提供 + 包更新):

{
  "name": "front-office",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.6",
    "@testing-library/react": "^11.2.2",
    "@testing-library/user-event": "^12.2.2",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.9",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "bootstrap": "^4.5.3",
    "react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Run Code Online (Sandbox Code Playgroud)

我正在使用最新版本的 Typescript,并且我使用ncu关闭/打开的 VSCode更新了我的所有包(有时使用 tsconfig !),但似乎没有任何解决方案。

我很确定这是一个 VSCode 问题,但我对如何解决这个问题没有想法。

你们有人有想法吗?

编辑:

如果您按照上述步骤操作并且 VSCode 仍然显示“ -jsx ”错误,请确保您已禁用“TypeScript God”扩展(以及任何其他 TS 扩展,直到问题不再出现)。

小智 123

这是因为 VSCode 的 Typescript 版本没有使用 create-react-app 默认使用的最新 babel 功能。

您可以更改 VS Code 以使用工作区的 Typescript 版本,这将解决此问题。

打开 TypeScript 或 JavaScript 文件,然后单击状态栏中的 TypeScript 版本号。将出现一个消息框,询问您应该使用哪个版本的 TypeScript VS Code

选择“使用工作区版本”以使用较新的 Create React App 打字稿版本。

  • 如果项目分为多个子文件夹,如何更改工作区版本?VSCode 似乎没有在子目录中拾取“node_modules”,或者至少我没有看到其他可用版本 (4认同)
  • 自上次拯救我的 a** 以来的天数:0 (3认同)
  • 似乎它是它和我正在使用的扩展(TypeScript God)的混合体,可能不是最新的。不管怎样,它解决了我的问题,谢谢! (2认同)

Att*_*emi 25

在 PhpStorm 中(对于 WebStorm 可能也是如此),我通过在远程 JSON 模式中勾选“始终下载最新版本的模式”来让它不抱怨“react-jsx”作为值。

需要打勾的选项

  • 确实,因为我自己在搜索 PhpStorm 时发现了这个问题,所以我想也许这也可以帮助其他人。此外,这可能有助于成为有关 vscode 的线索,该问题可能与 JSON 模式可能过时有关。 (9认同)
  • 当我使用 WebStorm 时,这确实对我有帮助。谢谢你! (2认同)

Vij*_*raj 6

正如其他人提到的,这是 create-react-app (CRA) 脚本的一个未决问题。但是,上述解决方案都不适合我。对我有用的唯一解决方案是在文件中添加以下配置.env(在项目根目录中)。

DISABLE_NEW_JSX_TRANSFORM=true
Run Code Online (Sandbox Code Playgroud)