使用react dom/client 时出现以下错误
Compiled with problems:X
ERROR in src/index.tsx:2:28
TS7016: Could not find a declaration file for module 'react-dom/client'. 'C:/Users/Mohamad/Desktop/HIS/Administration/Administration/Administration/Administration.Endpoints.WebUI/ClientApp/node_modules/react-dom/client.js' implicitly has an 'any' type.
Try `npm install @types/react-dom` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-dom/client';`
1 | import * as React from 'react';
> 2 | import { createRoot } from 'react-dom/client';
| ^^^^^^^^^^^^^^^^^^
3 | import App from './App';
4 |
5 |
使用@latest 安装@types/react dom
npm i @types/react-dom@latest
错误消息实际上说明了解决方案:
您需要安装软件包或在.d.ts
文件中定义
然而,就我的情况而言,我不得不同时做这两件事来解决这个问题。
安装软件包:
npm install @types/react-dom
在.d.ts
中定义
declare module 'react-dom/client';
我没有对此大做文章,而是在tsconfig.json文件中添加了以下语句来禁用"any"类型的错误:
"noImplicitAny":错误