[TypeScript Introduction] TypeScript Introduction - Enumeration (enum)

        TypeScript is a statically typed, optional programming language that adds new features such as type checking, interfaces, and enumerations to JavaScript, making development more efficient and code more robust. In TypeScript, an enum is a special data type that can be used to define a set of named constants, making the code more readable and maintainable.        

        Enumeration ( mei ju ): The meaning of enumeration is to enumerate one by one, to list all the situations, so when selecting values, only these few can be used, and the others will not work.

        Enumeration in computer language: Put all constants in a set, so that several constants become a set of related content.

Table of contents

1. Basic grammar of enumeration

Two, custom enumeration value

3. Access enumeration value

4. Reverse mapping

Five, const enumeration

6. Constant enumeration expression 

Seven, external enumeration


        A TypeScript enumeration (enum) is a data type used to define a collection of named constants. In TypeScript, enums provide a convenient way to name a limited set of values ​​and encapsulate them in a namespace.

1. Basic grammar of enumeration

        TypeScript enums use the keyword "enum" to define a new enumeration type. For example:

enum Direction {
  Up,
  Down,
  Left,
  Right
}

        This code defines an Directionenumerated type called , which contains four constants: Up, Down, Leftand Right. By default, the values ​​of these constants are auto-incremented starting from 0.

Case implementation:

enum Direction {
  Up,
  Down,
  Left,
  Right,
}

const move = (direction: Direction) => {
  switch (direction) {
    case Direction.Up:
      console.log('Moving up');
      break;
    case Direction.Down:
      console.log('Moving down');
      break;
    case Direction.Left:
      console.log('Moving left');
      break;
    case Direction.Right:
      console.log('Moving right');
      break;
    default:
      break;
  }
}

move(Direction.Up); // 打印 "Moving up"

        In this example, we define a Direction enumeration type, including Up, Down, Left and Right four elements. Then we define a move function that accepts a parameter of Direction type and outputs different messages according to different parameters. When we call move(Direction.Up), Moving up will be output, because the parameter passed at this time is the Up element in the enumeration type Direction. If the passed parameter is not any element of the Direction type, then there will be no output in the default branch of the switch statement.

Two, custom enumeration value

        You can override the default auto-increment behavior by customizing the enum value. For example:

enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}

        In this example, Direction.Upthe value of is 1, while the remaining constants have values ​​of 2, 3, and 4, respectively.

3. Access enumeration value

        To access enumeration values, just use dot notation. For example:

console.log(Direction.Up); // 输出1
console.log(Direction.Down); // 输出2

4. Reverse mapping

        TypeScript enumerations also provide a reverse mapping function, allowing you to obtain their corresponding constant names through enumeration values. For example:

console.log(Direction[1]); // 输出Up
console.log(Direction[2]); // 输出Down

Five, const enumeration

        TypeScript also supports const enums, where enums of this type are removed at compile time and inlined wherever they are referenced. This can improve performance and reduce the size of the generated code. For example:

const enum Direction {
  Up,
  Down,
  Left,
  Right
}

console.log(Direction.Up); // 直接输出0,没有额外的代码生成

6. Constant enumeration expression 

        A constant enumeration expression is an expression that can be evaluated at compile time. These expressions include numeric literals, other constant enumeration expressions, and some arithmetic operators. Using constant enumeration expressions can improve performance and reduce the size of the generated code. For example:

enum E {
  A = 1 << 0,
  B = 1 << 1,
  C = A | B
}

        In this example, E.Cthe value of the constant evaluates to 3 (i.e. 1|2) at compile time.

Seven, external enumeration

        External enumerations are defined using the declare keyword. Document description: External enumerations are used to describe the shape of existing enumeration types, which means that external enumerations are used to describe enumeration objects that exist in the current environment. One difference between an external enumeration and a normal enumeration is that an uninitialized enumeration member in the external enumeration will be treated as a calculated value, while it will be a constant in the normal enumeration.

declare enum Enum {
    A = 1,
    B,
    C = 2
}

        There is an important difference between external enumerations and non-external enumerations. In normal enumerations, members without initializers are treated as constant members. For non-constant outer enums, no initializers are considered computed.


        In summary, an enumerated type in TypeScript is a data type that contains a limited number of named constants, which are converted to corresponding JavaScript code after compilation. Enumerated types can help us define a set of meaningful constants that can be called by name or value, improving the readability and maintainability of the code. In TypeScript, enum types can be defined in two ways: numeric enums and string enums. Numeric enumerations use numbers as enumeration values, and string enumerations use strings as enumeration values. Enumeration types also support advanced features such as reverse mapping, enumeration member types, and enumeration merging.

Guess you like

Origin blog.csdn.net/dxt19980308/article/details/129798821