TypeScript Language Specification v1.5
TypeScript Language Specification v1.5
TypeScript Language Specification v1.5
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
var c = Color.Red;<br />
console.log(Color[c]); // Outputs "Red"<br />
9.2 Enum Members<br />
The body of an enum declaration defines zero or more enum members which are the named values of the<br />
enum type. Each enum member has an associated numeric value of the primitive type introduced by the<br />
enum declaration.<br />
EnumBody:<br />
EnumMemberList , opt<br />
EnumMemberList:<br />
EnumMember<br />
EnumMemberList , EnumMember<br />
EnumMember:<br />
PropertyName<br />
PropertyName = EnumValue<br />
EnumValue:<br />
AssignmentExpression<br />
Enum members are either constant members or computed members. Constant members have known<br />
constant values that are substituted in place of references to the members in the generated JavaScript<br />
code. Computed members have values that are computed at run-time and not known at compile-time.<br />
No substitution is performed for references to computed members.<br />
An enum member is classified as follows:<br />
<br />
<br />
<br />
If the member declaration specifies no value, the member is considered a constant enum<br />
member. If the member is the first member in the enum declaration, it is assigned the value zero.<br />
Otherwise, it is assigned the value of the immediately preceding member plus one, and an error<br />
occurs if the immediately preceding member is not a constant enum member.<br />
If the member declaration specifies a value that can be classified as a constant enum expression<br />
(as defined below), the member is considered a constant enum member.<br />
Otherwise, the member is considered a computed enum member.<br />
Enum value expressions must be of type Any, the Number primitive type, or the enum type itself.<br />
A constant enum expression is a subset of the expression grammar that can be evaluated fully at<br />
compile time. An expression is considered a constant enum expression if it is one of the following:<br />
<br />
A numeric literal.<br />
130