Using enumerations we can define constants with names. TypeScript supports numeric and string-based enumerations.
Digital enumeration
Declare an enumerated type that, if not assigned, defaults to numeric values and sums up from 0:
enum Months {
Jan,
Feb,
Mar,
Apr
}
Months.Jan === 0 // true
Months.Feb === 1 // true
Months.Mar === 2 // true
Months.Apr === 3 // true
// If the value is assigned from the first number, the subsequent values are incremented
enum Months {
Jan = 1,
Feb,
Mar,
Apr
}
Months.Jan === 1 // true
Months.Feb === 2 // true
Months.Mar === 3 // true
Months.Apr === 4 // true
Copy the code
String enumeration
enum TokenType {
ACCESS = 'accessToken',
REFRESH = 'refreshToken'
}
// Two different values can be written
console.log(TokenType.ACCESS === 'accessToken') // true
console.log(TokenType['REFRESH'= = ='refreshToken') // true
Copy the code
Heterogeneous enumeration
A mixture of numeric and string types can be used, but is not recommended
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES",}Copy the code
Computed and constant members
Each enumerator takes a value, which can be constant or computed.
Enumerators are treated as constants if:
- The first member of the enumeration and no initializer, in which case it is given the value 0:
enum E { X } // E.X is constant
Copy the code
- An enumerator does not have an initializer, but its predecessor is initialized with a constant:
//All enum members in 'E1' and 'E2' are constant.
enum E1 { X, Y, Z }
enum E2 {
A = 1, B, C
}
Copy the code
- Enumerator members are initialized with constant enumeration expressions.
Constant enumerated expressions are a subset of TypeScript expressions that can be evaluated at compile time. An expression is a constant enumerated expression when it satisfies one of the following conditions:
- An enumerated expression literal (mainly a string literal or a number literal)
enum E1 { one = 10, two, three }
enum E2 { one = 'one', two='two', three='three'}
Copy the code
- A reference to a previously defined constant enumerator (which can be defined in a different enumeration type)
// One is a reference to e2. one, and two is a reference to the current enumerator one
enum E3 { one = E2.one, two = 2 * one }
Copy the code
- A bracketed constant enumeration expression
// It can be a function call or an evaluation of an expression
function returnNumber (x: number) :number {
return x
}
enum E4 {
one = (function () { return 1 })(),
two = returnNumber(10),
three = (E1.one + E1.two) % E1.three
}
Copy the code
- Unary operator
The +, -, -
One of them applies to constant enumeration expressions
enum E5 {
one = ~E1.one,// take the inverse operation
two = +E1.two,
three = -E1.three,
four = void 0
}
Copy the code
- Constant enumeration expressions as binary operators
The +, -, *, /, %, < <, > >, > > >, &, |, ^
Operation object of.
enum E6 {
// Left shift operation
one = 2 << 5.// Right shift
two = 64 >> 5,
three = 64 >>> 5.// Or combine multiplicative operations
four = (one | two) * three
}
Copy the code
Enumerators in all other cases are treated as values that need to be computed:
enum FileAccess {
// constant members
None,
Read = 1 << 1,
Write = 1 << 2,
ReadWrite = Read | Write,
// computed member
G = "123".length
}
Copy the code
Reverse mapping
The so-called reverse mapping refers to enumeration values, which can be either positive Months.Jan or negative Months[1].
enum Months {
Jan = 1,
Feb,
Mar,
Apr
}
console.log(Months.Mar === 3) // true
console.log(Months[3= = ='Mar') // true
Copy the code
Compiled JavaScript code:
'use strict'
var Months;
(function (Months) {
Months[Months['Jan'] = 1] = 'Jan'
Months[Months['Feb'] = 2] = 'Feb'
Months[Months['Mar'] = 3] = 'Mar'
Months[Months['Apr'] = 4] = 'Apr'
})(Months || (Months = {}))
Copy the code
Tips:
- String enumerators do not generate reverse mappings.
- An enumeration type is compiled into an object that contains both a forward mapping (name -> value) and a reverse mapping (value -> name).
Const enumeration
Use the const modifier on enumerations:
enum Months {
Jan = 1,
Feb,
Mar,
Apr
}
const month = Months.Mar
Copy the code
Compiled content:
'use strict'
const month = 3 /* Mar */
Copy the code
There are no objects that the enumerated type should compile, just the month constant. This is where declaring enumerations with the const keyword comes in. Because the variable Month already uses enumerated types, TypeScript removes them at compile time, which is another way to improve performance.
Enumeration merger
Declaring enumerations of the same name separately will merge automatically:
enum Months {
Jan = 1,
Feb,
Mar,
Apr
}
enum Months {
May = 5,
Jun
}
console.log(Months.Apr) / / 4
console.log(Months.Jun) / / 6
Copy the code
Learning links
- www.tslang.cn/docs/handbo…
- www.imooc.com/wiki/typesc…