除非在“jsx”为“react-jsx”时提供了“--jsx”标志,否则无法使用 JSX

Fel*_*lHa 7 jsx typescript reactjs react-scripts

打字稿抛出:

除非提供了“--jsx”标志,否则无法使用 JSX。ts(17004)

更改tsconfig.json jsx为 后react-jsx,jsx 工作。yarn start将 tsconfig.jsonreact-jsx再次更改为。

react-scripts 已更新至 4.0.1。

包.json

"dependencies": {
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "semantic-ui-css": "^2.4.1",
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.2.2",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.9",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@typescript-eslint/eslint-plugin": "^4.8.1",
    "@typescript-eslint/parser": "^4.8.1",
    "eslint-config-react": "^1.1.7",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "^4.2.0",
    "typescript": "^4.1.2"
  },
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "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)

如何解决这个问题?

Fel*_*lHa 12

您可以通过以下方式解决此问题:

“Ctrl + Shift + P”或单击窗口右下角的 Typescript 版本。TypeScript:选择 TypeScript 版本使用工作区版本... 4.1.2"