- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在为我正在使用的库编写类型定义。库中的一个函数识别被整数点击的鼠标按钮:
//index.d.ts
export as namespace myLib;
// activates the library listening for a specific mouse button
function activate(button : number ) : void
我引入了一个枚举来让它变得更好:
//index.d.ts
export as namespace myLib;
export enum MouseButton {
LEFT = 1,
MIDDLE = 2,
RIGHT = 4
}
export function activate(button : MouseButton ) : void;
现在,当我导入这个函数并使用它时,一切都会编译,但我猜枚举在浏览器中执行时被剥离和未定义。错误消息显示 Cannot read property 'LEFT' of undefined
。
因此我重新整理了这些文件:
//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};从“myLib”导入 * 作为 myLib
。但这需要两次导入。引用 myLib.MouseButton
仍然编译但不运行。
有什么方法可以通过 import * as myLib
语句导入的 myLib
导入和引用 MouseButton
枚举?我不仅在寻找一个解释如何做的答案,而且还在寻找一个解释为什么我的解决方案不起作用或为什么不可能的答案。还感谢对解释问题的资源的提示
PS:我也尝试了此处建议的语法 re-export Typescript enum from namespace?但这也不起作用。
PPS:有问题的模块是 Angular 6 项目中使用的基础项目 (https://github.com/cornerstonejs/cornerstone) 中的 UMD 模块。
最佳答案
在 Romain Denau's comment 的帮助下解决了它多于。它把我推向了正确的方向:What code does the typescript compiler generate from an enum (see https://www.typescriptlang.org/docs/handbook/enums.html#enums-at-runtime)?声明枚举 const
允许 typescript 编译器将标识符与相应的值完全交换,有效地内联它。枚举不再泄漏到生产代码中。谢谢!
//index.d.ts
export as namespace myLib;
export const enum MouseButton {
LEFT = 1,
MIDDLE = 2,
RIGHT = 4
}
export function activate(button : MouseButton ) : void;
关于typescript - 从 typescript 类型定义文件导出枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50564756/
我是一名优秀的程序员,十分优秀!