As I compile the type definitions for a library that I'm utilizing, I came across one function that 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
To enhance readability, I introduced an enum:
//index.d.ts
export as namespace myLib;
export enum MouseButton {
LEFT = 1,
MIDDLE = 2,
RIGHT = 4
}
export function activate(button : MouseButton ) : void;
Upon importing and using this function, everything compiles successfully. However, during execution in the browser, the enum seems to be stripped away, resulting in the error message
Cannot read property 'LEFT' of undefined
.
In an attempt to resolve this issue, I restructured the files as follows:
//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;
Now, I can use
import {MouseButton} from "myLib/MouseButton"; import * as myLib from "myLib"
. Nonetheless, this solution necessitates two imports. Although referencing myLib.MouseButton
still compiles, it does not run as intended.
Is there a way to import and reference the MouseButton
enum through the myLib
imported via import * as myLib
? I am seeking an explanation not only on how to achieve this but also on why my initial solution failed or if such an approach is unfeasible. Any pointers to resources elucidating these issues would be highly appreciated.
PS: I also experimented with the syntax recommended here re-export Typescript enum from namespace?, yet it did not yield the desired outcome.
PPS: The module under consideration is a UMD module from the cornerstone project (https://github.com/cornerstonejs/cornerstone) incorporated into an angular 6 project.