Avatar ·

TypeScript和React中“Cannot use JSX unless the '--jsx' flag is provided”错误

大家好!我是TypeScript和React的新手,正在尝试设置项目,但遇到了一个我两天都没能解决的错误。 我有一个简单的React组件,写在.tsx文件中: ```tsx import React from 'react'; interface Props { name: string; } const Greeting: React.FC = ({ name }) => { return

Hello, {name}!

; }; export default Greeting; ``` 当我通过`tsc`运行编译时,出现错误: ``` Cannot use JSX unless the '--jsx' flag is provided. ``` 我使用的是TypeScript 5.0.3版本,React 18,我的tsconfig.json如下: ```json { "compilerOptions": { "target": "ES2020", "module": "ESNext", "strict": true, "esModuleInterop": true, "outDir": "./dist", "rootDir": "./src" }, "include": ["src/**/*"] } ``` 我尝试过谷歌搜索,但网上的建议各不相同:有人说设置`"jsx": "react"`,有人说设置`"jsx": "preserve"`,还有人...