【发布时间】:2021-03-06 12:51:53
【问题描述】:
我正在努力解决 VSCode 中的“错误”:
Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'
因此,当 react 值似乎有效时,react-scripts (create-react-app) 会自动将 jsx 键设置为 react-jsx 值。
实际上,代码可以完美运行并显示我想要的页面,但是 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 扩展,直到问题不再出现)。
【问题讨论】:
标签: typescript visual-studio-code jsx create-react-app tsconfig