typescript - Visual Studio Code 使用 "react-jsx"作为 jsx 值与 create-react-app 的问题

标签 typescript visual-studio-code jsx create-react-app tsconfig

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

Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'
因此,react-scripts (create-react-app) 会自动设置 jsx key react-jsx值,当 react值(value)似乎有效。
实际上,代码运行良好并显示了我想要的页面,但是 IDE 将所有内容都强调为错误,并说:
Cannot use JSX unless the '--jsx' flag is provided.
这是我的 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"
  ]
}

,和我的 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"
    ]
  }
}

我正在使用最新版本的 Typescript,并使用 ncu 更新了我的所有软件包,关闭/打开 VSCode(有时与 tsconfig 一起工作!)似乎没有任何解决方案。
我很确定这是一个 VSCode 问题,但我对如何解决这个问题没有想法。
你们有人有想法吗?
编辑:
如果您按照上述步骤操作并且 VSCode 仍然显示“-jsx”错误,请确保您已禁用“TypeScript God”扩展(以及任何其他 TS 扩展,直到问题不再出现)。

最佳答案

这是因为 VSCode 的 Typescript 版本没有使用 create-react-app 默认使用的最新 babel 功能。
您可以 change VS Code to use the workspace's version of Typescript instead ,这将解决此问题。

Open a TypeScript or JavaScript file and click on the TypeScript version number in the Status Bar. A message box will appear asking you which version of TypeScript VS Code should use


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

关于typescript - Visual Studio Code 使用 "react-jsx"作为 jsx 值与 create-react-app 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64974648/

相关文章:

python - VScode扩展: CODE RUNNER and PYTHON PREVIEWER do not use the interpreter I select

javascript - VSCode 中的 Intellisense 用于 Electron 应用程序开发

javascript - Undefined 不是评估 this.state/this.props 的对象

javascript - 在React中循环父对象内的对象

angular - 在 Angular 5 中呈现 View 之前等待数据

javascript - 通过检查是否存在一系列不起作用的字符,在 url 上运行 ".includes"

javascript - 将 Vue3 typescript 转换为 Vue3 Javascript

angular - 我们应该规范化 Angular 应用程序中的状态吗?

javascript - 如何在 React JSX 中添加空格

vim - 是否可以将 VSCode 语言-服务器-协议(protocol)引擎与 Vim 或 NeoVim 一起使用?