enum Alpha { X, Y, Z }
const enum Beta { X, Y, Z }
declare enum Gamma { X, Y, Z }
declare const enum Delta { X, Y, Z }

44791 次浏览

enum Foo { X, Y }

var Foo;
(function (Foo) {
Foo[Foo["X"] = 0] = "X";
Foo[Foo["Y"] = 1] = "Y";
})(Foo || (Foo = {}));

enum Foo { X = 4 }
var y = Foo.X; // emits "var y = 4";


declare enum Foo {
X, // Computed
Y = 2, // Non-computed
Z, // Computed! Not 3! Careful!
Q = 1 + 1 // Error
}

const enum Foo { A = 4 }
var x = Foo.A; // emitted as "var x = 4;", always

// Note: Assume no other file has actually created a Foo var at runtime
declare enum Foo { Bar }
var s = 'Bar';
var b = Foo[s]; // Fails


module MyModule {
// Claiming this enum exists with 'declare', but it doesn't...
export declare enum Lies {
Foo = 0,
Bar = 1
}
var x = Lies.Foo; // Depend on inlining
}


module SomeOtherCode {
// x ends up as 'undefined' at runtime
import x = MyModule.Lies;


// Try to use lookup object, which ought to exist
// runtime error, canot read property 0 of undefined
console.log(x[x.Foo]);
}


enum Cheese { Brie, Cheddar }

var Cheese;
(function (Cheese) {
Cheese[Cheese["Brie"] = 0] = "Brie";
Cheese[Cheese["Cheddar"] = 1] = "Cheddar";
})(Cheese || (Cheese = {}));

const enum Bread { Rye, Wheat }

Bread.Rye
Bread['Rye']

declare enum Wine { Red, Wine }

declare const enum Fruit { Apple, Pear }