没有ts版本的第三方库,提示 Could not find a declaration file for module
problem
没有ts版本的第三方库,提示 Could not find a declaration file for module
reason
ts需要类型定义
solution
方法1:
如果该库有对应的声明文件,只需要安装即可,一般npm包名是 @types/xxx
npm install -D @types/lodash
方法2:
如果js包没有定义类型,迂回的办法是:在全局类型type文件中声明js模块
// 常见的这种文件有:shims-vue.d.ts env.d.ts 没有可以自己定义
declare module "lodash";
注意:这样声明只是不让报错,如果想通过声明文件获得代码补全,参数提示等功能,需要自己编写类型声明,常用的语法有:
- declare var声明全局变量
- declare function 声明全局方法
- declare class声明全局类
- declare enum 声明全局枚举类型
- declare namespace 声明(含有子属性的)全局对象
- interface type 声明全局类型