解决cannot find module ***(file contains the enum)报错问题

今天遇到一个问题,一个第三方库的***.d.ts文件里定义了一个enum

export enum ABC{
    DD: 3
}

我没有办法import这个enum,在编译的时候会报错 cannot find module ***(file contains the enum)

原因是声明文件.d.ts是用来给js格式的内容添加类型辅助声明的,本身只放类型声明相关的东西,并且打包编译的时候不会被编译到包内。这样就容易理解了,enum并不是一个对某对象的类型声明,而是定义了一个对象,该内容打包时不被打包,引用这个enum的代码就无法找到这个enum导致报错。

官网对.d.ts文件的说明如下:

TypeScript has two main kinds of files. `.ts` files are _implementation_ files that contain types and executable code. These are the files that produce `.js` outputs, and are where you’d normally write your code.
`.d.ts` files are _declaration_ files that contain _only_ type information. These files don’t produce `.js` outputs; they are only used for typechecking.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值