Is there a way to create enums in typescript that are ambient?
enums, typescript
Solution
TypeScript 0.9 allows this:
declare module A { export enum E { X, Y, Z } }
Problem
edit after release of typescript 0.9: enums are now supported: ``` enum Select { every, first, last } ``` original question: Enums in typescript were discussed here but no solution leads to an ambient design. An ambient enum definition would mean that the enum is handled by the compiler only and the compiled js output files only deal with the raw numerical values. Like in C++11. The closest I get is ``` declare var Color = { red: 1, blue: 2, green: 3 } // DOES NOT COMPILE ``` but the compiler does not accept this: "Ambient variable cannot have initializer". edit incorporating dmck's answer: ``` declare var Color: { Red: number; Green: number; Blue: number; }; ``` This does not output any js code. In other words, it is ambient. This however makes it useless as well: ``` declare var Color: { Red: number; Green: number; Blue: number; }; function test() { var x = Color.Blue; // null ref exception, no Color object console.log(x == Color.Red); } ``` will produce a runtime error, as Color is not defined in js. The ts declaration just claims that there is some Color object in js, while in fact, without definition, there is none. Ti fix this we can add ``` var Color = { Red: 1, Green: 2, Blue: 3 }; ``` but now the enum implementation is not "ambient" in the sense that the typescript compiler does what a C++ compiler does, reducing the enum values at all occurencies to plain numbers. The current ambient declarations allow type checking but not such replacement.