TS7016: Could not find a declaration file for module xxx .

一、错误

TS7016: Could not find a declaration file for module crypto-js/md5 .

或者

TypeError: Cannot read property ‘CryptoJS’ of undefined

以加密库 crypto-js 为例,当你使用 TypeScript 时,直接按官方文档导入时,会发现不起作用

import MD5 from 'crypto-js/md5';

意思时缺少类型定义,再 TypeScript 环境下外部包导入都需要相应的定义文件(*.d.ts)

二、解决

使用 @types/库名 进行安装即可

npm install --save-dev @types/crypto-js

不得不说,前端越来越标准和严格了

参考链接:
https://github.com/brix/crypto-js/issues/168#issuecomment-455808128
https://zhuanlan.zhihu.com/p/349595729

Author: thinkwei

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注