Avatar ·

TypeScript 与 React 中的错误“React refers to a UMD global”

大家好!我是 TypeScript 和 React 的新手。通过 create-react-app 使用 TypeScript 模板创建了项目,但在尝试编写简单组件时遇到了一个奇怪的错误。 这是我的代码(App.tsx 文件): ```tsx import React from 'react'; import ReactDOM from 'react-dom'; const App: React.FC = () => { return

Hello World

; }; ReactDOM.render(, document.getElementById('root')); ``` TypeScript 在 `import React from 'react'` 这一行报错: "'React' refers to a UMD global, but the current file is a module. Consider adding an import instead." 我很困惑。我明明已经使用了 import,但编译器却说需要添加导入。尝试过重新安装依赖、删除 node_modules 和 package-lock.json——没有帮助。我使用的是最新版本的 React 和 TypeScript。请告诉我哪里做错了以及如何修复? tsconfig.json 看起来像这样(由 CRA 生成): ```json { "compilerOptions": { "target": "es