最佳答案
我纠结于 VSCode 中的以下“错误”:
Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'
因此,当 react
值似乎可以工作时,response-script (create-response-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-response-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"
]
}
}
我使用的是最新版本的 Type 脚本,并且我用 ncu
更新了所有的包,关闭/打开 VSCode (有时可以使用 tsconfig!)似乎没有什么能解决这个问题。
我很确定这是 VSCode 的问题,但是我已经没有办法解决这个问题了。
你们有主意吗?
编辑:
如果您按照上述步骤操作,并且 VSCode 仍然显示“ JSX”错误,请确保您已经禁用了“ TypeScript God”扩展(以及任何其他 TS 扩展,直到问题不再出现)。