Typescript Enumerations and External Modules (Browser)

So far, I have been using typescript internal modules and have included 10s of script tags to load my application. I'm in the process of converting a project to use external modules (using a browser), but I'm stuck with how I should convert the listings.

I had something like:

mymodule.submodule { enum MyEnum { one, two, three } } 

and I will use it in other modules, for example:

 var val:mymodule.submodule.MyEnum = mymodule.submodule.MyEnum.one; 

and this compiled for js correctly. After converting the project to using external modules, I moved all my interfaces to * .d.ts files and thought about putting enums in there, but of course this caused an error because there was no comparison between the enumeration and the number in js, then I moved the enumerations to * .ts files so that they are compiled. The problem is that if I write them as:

 export enum MyEnum{ one, two, three } export enum OtherEnum { four, five, six } 

which works to request enumerations 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 

BUT is not compatible with type mymodule.submodule.MyEnum . So, how can I have as an declaration for an enumeration type, so that I can declare variable types inside d.ts files, but also have a dependency on the actual generated enumeration code (so that it is loaded correctly) and used in the .ts file?

Note: these enumerations are used in many modules, so including them in the same file that they use is not a solution.

UPDATE about interface declarations: it was probably not clear why I saved the original listing declarations in d.ts files, so I add an example.

in .d.ts interfaces 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, I cannot use import instructions in .d.ts files, since I can use a single enum declaration in all cases?

+5
source share
1 answer

Enumerations must be in the TS file because the enumerations are compiled in JavaScript. When using modules, you must import the enumerations (even if they are used to determine the type of variable).

Change the code to the following:

 import Enums = require('./enums'); var val1: Enums.MyEnum = Enums.MyEnum.one; // no error 

In addition, enumerations should not be in mymodule.submodule if they are defined in enums.ts . Remove them from the definition file.

Regarding Update

To make enumerations work with interfaces, you can move the interfaces from the declaration file ( interfaces.d.ts ) and export them from the typescript file (for example, interfaces.ts ):

 import Enums = require('./enums'); export interface ISomething { aValue: Enums.MyEnum; aFunction: (anArg: Enums.MyEnum) => void; } 

Then you can use the following interfaces:

 import Interfaces = require('./interfaces'); import Enums = require('./enums'); var example: Interfaces.ISomething = { aValue: Enums.MyEnum.one, aFunction: (example: Enums.MyEnum) => { console.log('example'); } }; 
+1
source

All Articles