TypeScript fragrance series - Enumeration

Preface

The contents of TypeScript fragrance series will refer to Chinese document , but the examples in this paper will not be the same as those in the document, and some places will be studied in depth. In addition, the results of some examples are compiled into JavaScript after the code has no errors. If you want to actually see the code of TypeScript compiled into JavaScript, you can access the Online compilation address , hands-on operation, more impressive.

concept

Enumeration is an extension of JavaScript standard data type set, which is often used to limit the value of a certain range of scenarios. Enumeration of numbers and strings is supported in TypeScript. We can use enum.

Numeral enumeration

Let's first look at a numerical enumeration example:

enum Basketballer{ 
    Yao,
    Yi,
    Wang
}
Basketballer.Yao; //0
Basketballer.Yi;  //1
Basketballer.Wang;  //2

We can also do this. The following method is called reverse mapping, which is to access the key by value:

Basketballer[0]; //"Yao"
Basketballer[1]; //"Yi"
Basketballer[2]; //"Wang"
Basketballer[3]; //undefined

In the above example, we did not initialize the Yao or other members. If we do this:

enum Basketballer{ 
    Yao = 1,
    Yi,
    Wang
}
Basketballer.Yao; //1
Basketballer.Yi;  //2
Basketballer.Wang;  //3
Basketballer[1];  //"Yao"
Basketballer[2];  //"Yi"
Basketballer[3];  //"Wang"

As can be seen from the above, if a member has a value set, then the corresponding values will increase. However, it should be noted that if there is no special purpose, it is not necessary to manually assign multiple members. Because sometimes it may lead to some problems:

enum Basketballer{ 
    Yao = 1,
    Yi,
    Wang = 2,
    Zhu,
    Zhou,
}
Basketballer[2]; //"Wang"
Basketballer.Yi; //2

A simple application of enumeration:

enum Basketballer{ 
    Yao = 1,
    Yi,
    Wang,
    Zhu,
    Zhou,
}

function one(arg: Basketballer): string{
    let num = arg;
    switch (num) {
        case 1:
            return Basketballer[num] + "First person";
        case 2:
            return Basketballer[num] + "Second person";
        case 3:
            return Basketballer[num] + "Second person";
        default:
            return "Basketball";
    }
}
one(1); //"Yao first"
one(6); // "Basketball players"
one("1"); //Error parameter of type '1' cannot be assigned to basketball

In numerical enumeration, when we define enumeration member values, they can be constants or calculated:

Enumeration member value is constant

1. First member of enumeration and not initialized:

enum Basketballer{ 
    Yao
}
Basketballer.Yao; //0

2. Without initializer and its previous member is a numeric constant:

enum Basketballer1{ 
    Yao,  //0
    Yi,   //1
    Wang  //2 
}

enum Basketballer2{ 
    Yao = 1,  //1
    Yi,       //2
    Wang     //3 
}

3. Enumeration members are initialized with constant enumeration expressions. When one of the following conditions is met, it is a constant enumeration expression:
An enumeration expression literal (mainly string literal or number literal)

enum Man{ a = 1}

A reference to a previously defined constant enumeration member (can be defined in different enumeration types)

enum Man{ a = 1, b = 2*a}

Parenthesized constant enumeration expression

enum Man{ a = 1, b = 2*(a +1)}

One of the unary operators +, -, ~ is applied to constant enumeration expression

enum Man{ a = 1, b= ~a}

The constant enumeration expression is used as the operands of the binary operators +, -, *, /,%, <, >, > >, &, |, ^. If the constant enumeration expression evaluates to NaN or Infinity, an error is reported at compile time.

enum Man{ a = 1 << 2}

Enumeration member value is calculated value

Except that all enumeration members mentioned above are treated as the values to be calculated:

enum Man{ a = "abc".length }

String Enum

There is no auto increment feature in string enumeration. We must set a string for each member during initialization.

enum Basketballer{ 
    Yao = "Basketball Association President",
    Yi = "Card face",
    Wang = "Big brother",
}
Basketballer.Yao; //"Chairman of Basketball Association"

But string enumeration has no reverse mapping:

Basketballer["Basketball Association President"]; // error

Heterogeneous enumeration

Heterogeneous enumeration is the combination of numerical enumeration and string enumeration, but it is not recommended in official documents.

enum Basketballer{ 
    Yao = "Basketball Association President",
    Yi = 1,
}

const enumeration

We can use the const modifier to emphasize the current enumeration type. Const enumeration can only use constant enumeration expressions, which will be deleted during compilation. We can use const enum to define:

const enum Man {
    A,
    B,
    C,
    D
}
let man = [Man.A, Man.B, Man.C, Man.D];
console.log(man); // [0, 1, 2, 3]

Let's look at the compilation results:

let man = [0 /* A */, 1 /* B */, 2 /* C */, 3 /* D */];
console.log(man); // [0, 1, 2, 3]

External enumeration

External enumeration is used to describe the shape of existing enumeration types, which can be defined by declare enum:

declare enum Man {
    A,
    B,
    C,
    D
}
let man = [Man.A, Man.B, Man.C, Man.D];

Let's look at the compilation results:

let man = [Man.A, Man.B, Man.C, Man.D];

Reference resources

https://github.com/zhongsp/TypeScript
https://github.com/jkchao/typescript-book-chinese

Last

Some places in the article may be added with some understanding. If there are any inaccuracies or mistakes, please point out~

Published 8 original articles, won praise 8, visited 1534
Private letter follow

Tags: TypeScript Javascript github

Posted on Wed, 05 Feb 2020 21:01:13 -0800 by mikewooten