Is there a way to create enums in typescript that are ambient? -


edit after release of typescript 0.9: enums supported:

enum select { every, first, last } 

original question:

enums in typescript discussed here no solution leads ambient design. ambient enum definition mean enum handled compiler , compiled js output files deal raw numerical values. in c++11.

the closest is

declare var color = { red: 1, blue: 2, green: 3 } // not compile 

but compiler not accept this: "ambient variable cannot have initializer".

edit
incorporating dmck's answer:

declare var color: { red: number; green: number; blue: number; }; 

this not output js code. in other words, ambient. makes useless 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 runtime error, color not defined in js. ts declaration claims there color object in js, while in fact, without definition, there none. ti fix can add

var color = {    red: 1,    green: 2,    blue: 3 }; 

but enum implementation not "ambient" in sense typescript compiler c++ compiler does, reducing enum values @ occurencies plain numbers. the current ambient declarations allow type checking not such replacement.

typescript 0.9 allows this:

declare module { export enum e { x, y, z } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -