如何使用DefinitelyTyped 中的仅编译时枚举?
我想使用包 @types/bootstrap
在 Bootstrap 组件上创建事件侦听器时添加类型提示,例如 Carousel.Events.slide
枚举成员:
/* node_modules/@types/bootstrap/.../carousel.d.ts */
enum Events {
slide = 'slide.bs.carousel',
}
/* index.ts */
declare const Carousel: typeof import("bootstrap").Carousel;
myCarouselElement.addEventListener(Carousel.Events.slide, e => {
// compiler knows about 'relatedTarget' from 'carousel.d.ts'
const active = e.relatedTarget;
});
但是,因为该包导出一个常规枚举,发出的 JavaScript 保留 Carousel.Events.slide 调用(Bootstrap 中不存在),而不是为其提供slide.bs.carousel
值。除了不好的做法之外,我可以编辑 carousel.d.ts
文件的副本,并将 Events
声明为 const enum
,这将内联枚举值;然而,这有它自己的问题并且不是' t 是DefinitelyTyped 存储库的合适解决方案。
我应该如何正确导入枚举?我可以以某种方式将导入别名为 const enum
吗?
I want to use the package @types/bootstrap
to add type hinting when creating event listeners on Bootstrap components, such as the Carousel.Events.slide
enum member:
/* node_modules/@types/bootstrap/.../carousel.d.ts */
enum Events {
slide = 'slide.bs.carousel',
}
/* index.ts */
declare const Carousel: typeof import("bootstrap").Carousel;
myCarouselElement.addEventListener(Carousel.Events.slide, e => {
// compiler knows about 'relatedTarget' from 'carousel.d.ts'
const active = e.relatedTarget;
});
However, since the package exports a regular enum, the emitted JavaScript retains the Carousel.Events.slide
call (which doesn't exist in Bootstrap), rather then supplying it with the slide.bs.carousel
value. Bad practices aside, I could edit my copy of the carousel.d.ts
file and declare Events
as a const enum
, which would inline the enum value; however, this has its own problems and isn't a suitable solution for the DefinitelyTyped repo.
How should I correctly be importing enums? Can I somehow alias my import as a const enum
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论