Climbing the TS Road Type Alias, Literal Type, Enumeration

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.

type Name = string


const myname1: Name = 'clz'
const myname2: Name = 123
copy

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.

type Name = string


const myname1: Name = 'clz'
const myname2: Name = 123
copy

literal type

Literal types can be used to constrain values ​​to be specific literals only.

type 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'
}
copy

null and undefined are unrestricted

type Name = 'clz' | 123 | true | { name: 'clz' }

const myname1: Name = null
const myname2: Name = undefined
copy

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:

enum 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
copy

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

console.log(Direction['East'])     // 0
console.log(Direction[0])          // East
copy

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

enum Direction {
    East = 4,
    South = 1,
    West,
    North
}

console.log(Direction)
copy

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.

enum 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
copy

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.

const enum Direction {
    East,
    South,
    West,
    North,
}
copy

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

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

enum Direction {
    East = -4,
    South = -1.5,
    West,
    North
}

console.log(Direction)  
copy

enumeration item is not a number

Manually assigned enumeration items may not be numbers

enum Direction {
    East = "E",
    South = "S",
    West = "W",
    North = "N"
}

console.log(Direction)  
copy

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.

enum Direction {
    East = "E" as any,
    South = "S" as any,
    West = "W" as any,
    North = "N" as any
}

console.log(Direction)     
copy

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

Tags: string null undefined

Posted by facets on Thu, 16 Mar 2023 17:22:23 +1030