从打字稿类型定义文件导出枚举 [英] exporting enum from typescript type definition file

查看:25
本文介绍了从打字稿类型定义文件导出枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在使用的库编写类型定义.库中的一个函数标识一个整数点击的鼠标按钮:

I'm writing the type definitions for a library I'm using. One function in the library identifies the mouse button clicked by an integer:

 //index.d.ts
 export as namespace myLib;
 // activates the library listening for a specific mouse button
 function activate(button : number ) : void 

我引入了一个枚举来使它更好:

I introduced an enum to make this nicer:

//index.d.ts
export as namespace myLib;
export enum MouseButton {
    LEFT = 1,
    MIDDLE = 2,
    RIGHT = 4
}

export function activate(button : MouseButton ) : void;

现在,当我导入这个函数并使用它时,一切都会编译,但我猜枚举在浏览器中执行时会被剥离和未定义.错误消息说无法读取未定义的属性LEFT".

Now, when I import this function and use it, everything compiles but I guess the enum is stripped and undefined when executed in the browser. The error message says Cannot read property 'LEFT' of undefined.

因此我像这样重新排列文件:

Therefore I rearranged the files like so:

//MouseButton.ts
export enum MouseButton {
    LEFT = 1,
    MIDDLE = 2,
    RIGHT = 4
}

//index.d.ts
export as namespace myLib;
import {MouseButton} from MouseButton;
export {MouseButton} from MouseButton;
export function activate(button : MouseButton ) : void;

现在我可以从myLib/MouseButton"导入{MouseButton};import * as myLib from "myLib".但这需要两次导入.引用 myLib.MouseButton 仍然可以编译但不运行.

Now I can import {MouseButton} from "myLib/MouseButton"; import * as myLib from "myLib". But this requires two imports. Referencing myLib.MouseButton still compiles but doesn't run.

有没有办法通过 import * as myLib 语句导入的 myLib 导入和引用 MouseButton 枚举?我不仅在寻找解释如何做的答案,而且在寻找解释为什么我的解决方案不起作用或为什么不可能的答案.对解释错误的资源的提示也表示赞赏

Is there any way to import and reference the MouseButton enum via the myLib imported via the import * as myLib statement? I'm not only looking for an answer explaining how to do it but for one explaining why my solution doesn't work or why it isn't possible. Hints to resources explaining what's wrong are also appreciated

PS:我还尝试了此处建议的语法 从命名空间重新导出 Typescript 枚举? 但这也不起作用.

PS: I also tried the syntax suggested here re-export Typescript enum from namespace? but that didn't work either.

PPS:有问题的模块是来自基石项目的 UMD 模块 (https://github.com/cornerstonejs/cornerstone) 用于 angular 6 项目.

PPS: The module in question is a UMD module from the cornerstone project (https://github.com/cornerstonejs/cornerstone) used in an angular 6 project.

推荐答案

(完成t.animal自己的回答)

声明文件很难制作:请参阅冗长的文档.有时查看现有的 .d.ts 文件会有所帮助.

Declaration files are difficult to make: see the long documentation. Sometimes looking in existing .d.ts files can help.

关于enum,将它们声明为const enum 是一种干净而简单的方法.它是例如,为 jquery 做了什么,参见 @types/jquery/index.d.ts 用于 MouseKey.这很方便,因为标准枚举在 JavaScript 中编译为数组,而 const enum 成员直接编译为值;见 TypeScript Playground.

Regarding enum, declaring them as const enum is a clean and simple approach. It's what is done for jquery for instance, see @types/jquery/index.d.ts for Mouse and Key. It's handy because standard enums are compiled in JavaScript as arrays while const enum members are compiled directly as values ; see TypeScript Playground.

这篇关于从打字稿类型定义文件导出枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆