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

Cannot use JSX unless the '--jsx' flag is provided when "jsx" is "react-jsx"

打字稿抛出:

Cannot use JSX unless the '--jsx' flag is provided.ts(17004)

tsconfig.json jsx 更改为 react-jsx 后,jsx 有效。 yarn start 再次将 tsconfig.json 更改为 react-jsx

react-scripts 已更新至 4.0.1。

package.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"
  },

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"
  ]
}

如何解决这个问题?

你可以解决这个 following way:

"Ctrl + Shift + P" or Click Typescript version at the bottom right of the window. TypeScript: Select TypeScript Version Use Workspace Version... 4.1.2"

这是由于一个新错误,vscode 和打字稿不匹配。如果其他解决方案不起作用;我把tsconfig.json里面的include: "src"改成了

"include": [
     "./src/**/*.ts"
]