Previously, I utilized TypeScript internal modules and included numerous script tags to initialize my app. Now, I am in the process of transitioning the project to utilize external modules (using browserify), but I have hit a roadblock when it comes to converting enums.
Originally, my setup looked like this:
mymodule.submodule {
enum MyEnum {
one,
two,
three
}
}
I would use the enums in other modules like so:
var val: mymodule.submodule.MyEnum = mymodule.submodule.MyEnum.one;
After moving the project to external modules, I shifted all interfaces into *.d.ts files and attempted to include the enums there as well. However, this approach led to an error due to the lack of mapping between enum and number in JavaScript. Subsequently, I moved the enums to *.ts files for better compilation. The dilemma arises when I define them as:
export enum MyEnum{
one,
two,
three
}
export enum OtherEnum {
four,
five,
six
}
This method works for importing the enums in my code like:
import Enums = require('./enums');
var val = Enums.MyEnum.one; //this works
var val1: mymodule.submodule.MyEnum = Enums.MyEnum.one; //ERROR: incompatible types
However, it clashes with the mymodule.submodule.MyEnum
type. So, how can I declare the enum type for variable types in d.ts files while ensuring the generated enum code is correctly loaded and used in .ts file?
Note: These enums are utilized across multiple modules, making including them in the same file they are used not a viable solution.
UPDATE regarding interface declarations: It might not have been evident why I retained the original enum declarations in d.ts files, so here's an example.
In interfaces.d.ts, I currently have:
declare module mymodule.submodule {
enum MyEnum{
one,
two,
three
}
interface ISomething{
aValue: MyEnum;
aFunction: (anArg: MyEnum) => void;
}
}
As far as I know, import statements cannot be used in .d.ts files. How then, can I use one enum declaration universally?