preface

The TypeScript authoring series will refer to the Chinese documentation, but the examples in this article will not be duplicated, and some areas will be explored in depth. In addition, the results of some of the examples in this article were obtained after the code was compiled to JavaScript without errors. If you want to actually see TypeScript compiled into JavaScript code, you can visit TypeScript’s online compilation address to get a hands-on look.

concept

Enumerations are extensions to the standard set of JavaScript data types and are often used to limit scenarios to values within a range. Enumerations of numbers and strings are supported in TypeScript. We can do that with enum.

Digital enumeration

Let’s look at an example of a number enumeration:

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

We can also do this with a method called reverse mapping, which accesses keys by value:

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

In the above example, we did not initialize Yao or any other member. If we did:

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

As you can see from the above, if a member is set to a value, the subsequent values are incremented. Note, however, that you should never manually assign multiple members unless you have a specific purpose. Because sometimes it can cause some problems:

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

A simple use 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 man";
        case 2:
            return Basketballer[num] + "Second man";
        case 3:
            return Basketballer[num] + "Second man";
        default:
            return "Basketball";
    }
}
one(1); //"Yao First person"
one(6); // "basketball man"
one("1"); // Error, parameters of type "1" cannot be assigned to Basketballer
Copy the code

In numeric enumerations, we define enumerator values that are either constant or computed:

Enumerator values are constant

First member of enumeration and not initialized:

enum Basketballer{ 
    Yao
}
Basketballer.Yao; / / 0
Copy the code

2, no initializer and its preceding member is a numeric constant:

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

enum Basketballer2{ 
    Yao = 1./ / 1
    Yi,       / / 2
    Wang     / / 3
}
Copy the code

3. Enumerators are initialized with constant enumeration expressions. A constant enumeration expression is a constant enumeration expression when one of the following conditions is met: an enumeration expression literal (mainly a string literal or numeric literal)

enum Man{ a = 1}
Copy the code

A reference to a previously defined constant enumerator (which can be defined in a different enumeration type)

enum Man{ a = 1, b = 2*a}
Copy the code

A bracketed constant enumeration expression

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

One of the unary operators +, -, ~ applies to constant enumeration expressions

enum Man{ a = 1, b= ~a}
Copy the code

Constant enumeration expression as a binary operator, +, -, *, /, %, < <, > >, > > >, &, |, ^ operation object. If the constant enumeration expression evaluates to NaN or Infinity, an error is reported at compile time.

enum Man{ a = 1 << 2}
Copy the code

Enumerator values are computed values

All enumerators except those listed above are treated as values that need to be computed:

enum Man{ a = "abc".length }
Copy the code

String enumeration

There is no increment in string enumerations, so we must set each member to a string at initialization.

enum Basketballer{ 
    Yao = "President of the Basketball Association.",
    Yi = "Faces",
    Wang = "Big Brother",
}
Basketballer.Yao; //" President of basketball Association"
Copy the code

But string enumerations have no reverse mapping:

Basketballer["President of the Basketball Association."]; / / error
Copy the code

Heterogeneous enumeration

Heterogeneous enumerations are equivalent to a combination of numeric and string enumerations, but the official documentation does not recommend this.

enum Basketballer{ 
    Yao = "President of the Basketball Association.",
    Yi = 1,}Copy the code

Const enumeration

We can use the const modifier to highlight the current enumeration type. Const enumerations can only be used with expressions that use constant enumerations and are deleted at compile time. We can define this as const enum:

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

Let’s look at the compilation result:

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

External enumeration

External enumerations are used to describe the shape of an existing enumeration type. They can be defined with declare enum:

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

Let’s look at the compilation result:

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

reference

Github.com/zhongsp/Typ… Github.com/jkchao/type…

The last

Some of the article may add some of my own understanding, if there is inaccurate or wrong places, please point out ~