typescript项目安装完依赖import时显示Could not find

子成君 602 0

报错信息

Could not find a declaration file for module 'vue-touch'. 'C:/Users/xjq/Desktop/tmp/my-demo/node_modules/vue-touch/dist/vue-touch.js' implicitly has an 'any' type.  
Try `npm install @types/vue-touch` if it exists or add a new declaration (.d.ts) file containing `declare module 'vue-touch';`

提示你安装声明依赖或者自己添加一个声明文件。

解决方法

注:以下均以安装引入vue-touch为例
从 npm 安装第三方库时,还要同时安装这个库的ts类型声明文件。
1.从 TypeSearch 中找到并安装这些第三方库的类型声明文件,TypeSearch地址:https://microsoft.github.io/TypeSearch/
typescript项目安装完依赖import时显示Could not find
2.如果没有这个库的声明文件的话,我们需要手动声明这个库。
src目录下新建一个types目录,然后在types 目录下新建一个 index.d.ts文件
typescript项目安装完依赖import时显示Could not find
//index.d.ts
declare module "vue-touch";

错误原因

因为第三方类库并没有ts的.d.ts 类型的声明文件,所以无法在目前的项目中正常使用(具体可打开node_modules下对应包的文件夹查看)

发表评论 取消回复
OwO 图片 链接 代码

分享