Climbing the TS Road (6) Type Alias, Literal Type, Enumeration
type alias
Type aliasing is giving a type a new name. Use the keyword type.
copytype Name = string const myname1: Name = 'clz' const myname2: Name = 123

In the above example, a type alias is used, so the type alias Name can be directly used later as a string.
If you use type aliases for more complex types, it will be convenient for subsequent use.
copytype Name = string const myname1: Name = 'clz' const myname2: Name = 123
literal type
Literal types can be used to constrain values to be specific literals only.
copytype Name = 'clz' | 123 | true | { name: 'clz' } const myname1: Name = 'clz' const myname2: Name = 123 const myname3: Name = true const myname4: Name = { name: 'clz' } const myname5: Name = 'ccc' const myname6: Name = 124 const myname7: Name = false const myname8: Name = { name: 'czh' }

null and undefined are unrestricted
copytype Name = 'clz' | 123 | true | { name: 'clz' } const myname1: Name = null const myname2: Name = undefined
enumerate
Enumerations are generally used to represent a set of constants, such as the seven days of the week, and directions such as southeast, north, and south.
basic use
It's easy to use:
copyenum Direction { East, South, West, North } console.log(Direction['East']) // 0 console.log(Direction['South']) // 1 console.log(Direction['West']) // 2 console.log(Direction['North']) // 3
Enumeration members are assigned values that increment from 0, so the above example prints 0, 1, 2, and 3 in sequence. In addition, the enumeration value is also reversely mapped to the enumeration name. For example, the value of the enumeration member East is 0, then the value of Direction[0] is East
copyconsole.log(Direction['East']) // 0 console.log(Direction[0]) // East
So, how is this possible? Print Direction first and voila.

It is found that the keys and values of this enumeration object are properties of the object. Next, of course, we have to look at how it is implemented after compiling into JS.

It was found to be quite simple. That is, when assigning a value, use another assignment expression as a key to assign a value, so that the result of that expression will be used as a key.
manual assignment
copyenum Direction { East = 4, South = 1, West, North } console.log(Direction)
When manually assigning values, the value of the enumeration item will no longer increase from 0, but will increase after the previous enumeration item. For example, in the above example, the value of South is 1, and the value of West is not manually assigned, so its value is 2. Similarly, the value of North is 3.
Note: When non-manually assigned enumeration items and manually assigned enumeration items are repeated, the latter will overwrite the former.
copyenum Direction { East = 2, South = 1, West, North } console.log(Direction[2]) // West console.log(Direction) // The result when the key is 2 is West. The key is 2, and the value East is overwritten
constant enumeration
A constant enumeration is an enumeration type defined using const enum. It is different from normal enumeration, it will be deleted at compile time.
So the following code compiles to nothing.
copyconst enum Direction { East, South, West, North, }

copyconsole.log([Direction.East, Direction.South, Direction.West, Direction.North])

When we use it, the compiled result will be followed by the key of the enumeration item as a comment.
Other usage
enumeration item is a decimal or negative number
Manually assigned enumeration items can also be decimals or negative numbers, and the increment step is still 1
copyenum Direction { East = -4, South = -1.5, West, North } console.log(Direction)

enumeration item is not a number
Manually assigned enumeration items may not be numbers
copyenum Direction { East = "E", South = "S", West = "W", North = "N" } console.log(Direction)

There is no reverse mapping at this time, if the enumeration item is not a number, but you still want to have a reverse mapping. You need to use type assertion to let tsc ignore type checking, and all must and can only be asserted as any.

copyenum Direction { East = "E" as any, South = "S" as any, West = "W" as any, North = "N" as any } console.log(Direction)

When the manually assigned enumeration item is not a number, there cannot be non-manually assigned items behind it
