我在VSCode中遇到以下"错误":
Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'
因此,react-scripts(create-react-app)在react
值似乎可行时,自动将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扩展)。
node_modules
,或者至少我没有看到其他可用的版本。 - Narigonpm i -g typescript@4.1.2
),然后将以下这行代码添加到 vscode 的设置 json 文件中"typescript.tsdk": "/home/yourusername/.nvm/versions/node/v10.15.1/lib/node_modules/typescript/lib"
请将/home/yourusername/.nvm/versions/node/v10.15.1/lib/node_modules
替换成你全局 npm node_modules 文件夹的路径(可以用npm root -g
命令查找)。 - S..settings.json
中的设置更改为使用本地的typescript版本:"typescript.tsdk": "./node_modules/typescript/lib"
。 - undefined