11. Enumerations¶
An enumeration type enum specifies a distinct type with an associated set of named constants that define its possible values:
enumDeclaration:
'enum' identifier '{' enumConstantList '}'
;
enumConstantList:
enumConstant (',' enumConstant)* ','?
;
enumConstant:
identifier ('=' constantExpression)?
;
Qualification by type is mandatory to access the enumeration constant:
1 enum Color { Red, Green, Blue }
2 let c: Color = Color.Red
If enumeration type is exported, then all enumeration constants are exported along with the mandatory qualification.
For example, if Color is exported, then all constants like Color.Red are exported along with the mandatory qualification Color.
The value of an enum constant can be set explicitly to a numeric constant
expression (expression of type int
) or to a constant expression
of type string
, or implicitly by omitting the constant expression. If
the constant expression is omitted, then the value of the enum constant
is set implicitly to a numeric value (see Enumeration Integer Values).
A compile-time error occurs if integer
and string
type enumeration
constants are combined in one enumeration.
Any enumeration constant is of the enumeration type. The implicit conversion (see Enumeration to Int Conversions, Enumeration to String Conversions) of the enumeration constant to types integer or string depends on the type of enumeration.
In addition, all enumeration constant names must be unique. Otherwise, a compile-time error occurs.
1 enum E1 { A, B = "hello" } // compile-time error
2 enum E2 { A = 5, B = "hello" } // compile-time error
3 enum E3 { A = 5, A = 77 } // compile-time error
4 enum E4 { A = 5, B = 5 } // OK! values can be the same
11.1. Enumeration Integer Values¶
The integer value of an enum constant is set implicitly if an enumeration constant specifies no value.
A constant expression of type int
—a signed 32-bit integer (see
Integer Types and Operations for details)—can be used to set
the value explicitly:
1 enum Background { White = 0xFF, Grey = 0x7F, Black = 0x00 }
If all constants have no value, then the first constant is assigned the value zero. The other constant is assigned the value of the immediately preceding constant plus one.
If some but not all constants have their values set explicitly, then the values of the constants are set by the following rules:
The value of the first constant without an explicit value is assigned to zero.
A constant with an explicit value has that explicit value.
A constant that is not the first and has no explicit value takes the value of the immediately preceding constant plus one.
In the example below, the value of Red
is 0, of Blue
, 5, and of
Green
, 6:
1 enum Color { Red, Blue = 5, Green }
11.2. Enumeration String Values¶
A string value for enumeration constants must be set explicitly:
1 enum Commands { Open = "fopen", Close = "fclose" }
11.3. Enumeration Operations¶
The value of an enumeration constant can be converted to type string by
using the method toString
:
1 enum Color { Red, Green = 10, Blue }
2 let c: Color = Color.Green
3 console.log(c.toString()) // prints: 10
The name of enumeration type can be indexed by a value of this enumeration type to get the name of the constant:
1 enum Color { Red, Green = 10, Blue }
2 let c: Color = Color.Green
3 console.log(Color[c]) // prints: Green
The additional methods available for enumeration types and constants can be found in Enumeration Methods in the chapter Experimental Features.