使用CRA初始化了一个 React 18 的 TS 项目,配置好 mock-service-plugin mock 服务后想起项目体验下就收到下面错误

错误信息如下
error TS7016: Could not find a declaration file for module 'mock-service-plugin'.
'app-ts/node_modules/mock-service-plugin/src/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/mock-service-plugin` if it exists
or
add a new declaration (.d.ts) file containing `declare module 'mock-service-plugin';`
大致意思是:
安装的 mock-service-plugin这个包没有类型文件,
如果有 @types/mock-service-plugin 这个类型文件就安装下,
没有的话,就创建一个(.d.ts)文件 对这个包 声明下 declare module 'mock-service-plugin';
然后我就照做了如下图
创建了一个 react-app-env.d.ts 并把包添加进入

如果仅仅是这样的话,报错依然存在,要解决这个问题 需要在tsconfig.json中的 files 字段,将我们创建的 react-app-env.d.ts 包含进来 如下图,这一步是解决问题的关键


在使用CRA初始化的React18TypeScript项目中,遇到mock-service-plugin模块缺少类型定义的错误。按照提示尝试安装@types/mock-service-plugin未果后,创建了一个react-app-env.d.ts文件来声明模块,但仅此还不够,还需要在tsconfig.json的files数组中包含这个新创建的文件,才能成功解决报错。
5165

被折叠的 条评论
为什么被折叠?



