[Basic Study Notes enum] Introduction to enum enumeration type in TypeScript

Because many blogs I checked online only talked about the most basics, I will record them here and put the most basics at the end.

The important thing to record here is that the value of an enumeration member can be a string (string enumeration, because most of the Internet only introduces constant enumeration). One thing to note is that unlike numeric values: string enumeration does not grow automatically. For behavior, enumeration members must have initial values ​​set.

string enum

By default, enumeration member values ​​are numbers, but you can also use strings or mixed values ​​as enumeration member values. for example:

enum ErrorCode {
  NotFound = "404",
  ServerError = "500",
  Unauthorized = "401"
}

console.log(ErrorCode.NotFound);  // 输出: "404"

constant enumeration

Constant enumerations are removed at compile time, leaving only the values ​​of the enumeration members. This can reduce the amount of compiled code and is suitable for situations where only values ​​are needed but not names. for example:

const enum Size {
  Small = "S",
  Medium = "M",
  Large = "L"
}

let selectedSize: Size = Size.Medium;
console.log(selectedSize);  // 输出: "M"

Specify the value of an enumeration member

You can explicitly specify the value after enumerating the member. If not specified, it will be incremented by default. for example:

enum LogLevel {
  Error = 1,
  Warn,
  Info,
  Debug
}

let currentLogLevel: LogLevel = LogLevel.Info;
console.log(currentLogLevel);  // 输出: 3

reverse mapping

When the enumeration type is compiled into JavaScript, a two-way mapping is generated. The corresponding value can be obtained by the name of the enumeration member, or the corresponding enumeration member can be obtained by the value. for example:

enum Weekday {
  Sunday,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
}

console.log(Weekday.Sunday);  // 输出: 0
console.log(Weekday[0]);      // 输出: "Sunday"

enum as type

The enumeration type itself can be used as a type to constrain the value range of the variable. for example:

enum Gender {
  Male,
  Female
}

interface Person {
  name: string;
  age: number;
  gender: Gender;
}

let person: Person = {
  name: "Alice",
  age: 30,
  gender: Gender.Female
};

Basic usage

Basic usage is as follows:

enum Direction {
  Up,
  Down,
  Left,
  Right
}

let userDirection: Direction = Direction.Left;
console.log(userDirection);  // 输出: 2

In the above code, the Direction enumeration type defines four members: Up, Down, Left and Right. Each member is assigned a default numeric value, starting from 0 and increasing. When defining enumeration variables, you can directly use member names to assign values.

Since the enum type is not used much, I will record the learning.

The descriptions of the enum type on the Internet are almost the same. They are just that the default value of the defined enumeration member increases from zero. If a value is specified, it increases from the specified value. When using enumeration types, you can declare variables and assign enumeration values ​​to them just like normal types.

In TypeScript, you can use the enum keyword to define enumeration types. Enumeration types are used to define a set of named constant values, making the code more readable and maintainable. The enumeration (Enum) type is used in scenarios where the values ​​are limited to a certain range. For example, the gender is only male and female, and there are only a fixed number of departments in an organization (without considering the reorganization and reorganization of departments).

Basic usage is that each member of an enumeration type is assigned a default numeric value, starting from 0 and increasing. When defining enumeration variables, you can directly use member names to assign values.

I hope this article is helpful to you, and I also hope that the experts who pass by can give me some advice!

Guess you like

Origin blog.csdn.net/JaneLittle/article/details/132233755