使用react-dom/client出现TS7016错误:找不到模块声明文件
解决React DOM Client的TS7016类型声明错误
错误信息
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.
Trynpm install @types/react-domif it exists or add a new declaration (.d.ts) file containingdeclare module 'react-dom/client';
1 | import * as React from 'react';
import { createRoot } from 'react-dom/client'; ^^^^^^^^^^^^^^^^^^ import App from './App'; 5 |
解决方案
方法1:安装官方类型声明包
执行命令安装@types/react-dom:
npm install @types/react-dom --save-dev
使用Yarn的话:
yarn add @types/react-dom --dev
安装完成后重启开发服务器或重新编译项目,错误即可解决。
方法2:手动添加声明文件
若安装类型包无效,可手动创建声明文件:
- 在项目
src目录下新建react-dom-client.d.ts文件 - 写入以下内容:
declare module 'react-dom/client';
保存后TypeScript会自动读取该声明,消除类型报错。
内容的提问来源于stack exchange,提问作者Mohammad Ramezani
相关产品推荐
相关产品推荐

