我正在努力解决 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/