Using enumerations we can define constants with names. You can also express intent clearly or create a differentiated set of use cases. TypeScript supports numeric and string-based enumerations.

1. Enumeration of numbers

enum POSITION{
    TOP,
    RIGHT,
    BOTTOM,
    LEFT
}
let posT:POSITION = POSITION.TOP
let posR:POSITION = POSITION.RIGHT
let posB:POSITION = POSITION.BOTTOM
let posL:POSITION = POSITION.LEFT
console.log('posT=',posT)// posT = 0
console.log('posR=',posR)// posR = 1
console.log('posB=',posB)// posB = 2
console.log('posL=',posL)// posL = 3
Copy the code

By default, the initial value of TOP is 0, and the remaining members automatically grow from 1. The above enumeration example, running on TypeScript Playground, results in the following:

2. Enumeration of strings

In a string enumeration, each member must be initialized with either a string literal or another string enumeration member.

enum POSITION{
    TOP="TOP",
    RIGHT="RIGHT",
    BOTTOM="BOTTOM",
    LEFT="LEFT"
}
let posT:POSITION = POSITION.TOP
let posR:POSITION = POSITION.RIGHT
let posB:POSITION = POSITION.BOTTOM
let posL:POSITION = POSITION.LEFT
console.log('posT=',posT)// posT = TOP
console.log('posR=',posR)// posR = RIGHT
console.log('posB=',posB)// posB = BOTTOM
console.log('posL=',posL)// posL = LEFT
Copy the code

After compiling the above enumeration example, the corresponding ES5 code looks like this:

By looking at the compilation results of numeric enumerations and string enumerations, we can see that numeric enumerations are not supportedFrom the member name to the member valueIn addition to normal mapping, it also supportsFrom the member value to the member name, as shown below:

enum POSITION{ TOP, RIGHT, BOTTOM, LEFT} let posVal:POSITION = POSITION.TOP let posName = POSITION[0]// let posName:POSITION = POSITION[0 'string' is not assignable to tye "POSITION" console.log('posVal=',posVal)//posVal = 0 console.log('posName=',posName)//posName = TOPCopy the code

Constant enumerations are declared with the keyword const. Constant enumerations use inline syntax, do not compile for enumerated types, and do not generate any JS code. Specific examples are as follows:

const enum POSITION{
    TOP,
    RIGHT,
    BOTTOM,
    LEFT
}
let pos:POSITION = POSITION["TOP"]//0
Copy the code

After compiling the above enumeration example, the corresponding ES5 code looks like this:

The members of a heterogeneous enumeration are a mixture of strings and numbers, as shown below:

enum C{
    a,
    b,
    c="c",
    d=8
}
Copy the code

After compiling the above enumeration example, the corresponding ES5 code looks like this:

Note: Enumeration without initializer, eitherPut it in the first place, orIs initialized using a numeric constant or other constantAfter the enumeration of.

function getSomeValue(){
    return 123
}
enum C{
    a,
    A=getSomeValue(),
    B 
}
Copy the code

Enum member must have initializer. Because B is not an initialized variable, it’s a function that returns a value

function getSomeValue(){
    return 123
}
enum D{
    a=getSomeValue(),
    A=1,
    B 
}
console.log(D['a'],D['A'],D['B'])// 123 1 2
Copy the code

This is ok, because A has an initializer, and B follows A.

Links: # a great TS tutorial (1.8W word) # a good TypeScript summary