TypeScript Initial Knowledge - Enumeration

Number type enumeration

Conventional enumeration values are numeric types and are therefore referred to as numeric type enumerations:

enum Color {
    Red,
    Blue,
    Green
}
console.log(Color.Red);
// 0
console.log(Color[0]);
// Red
console.log(Color[Color.Red]);
// Red, Color.Red = 0

Change the number associated with the number enumeration

By default, the first enumeration value is 0, and subsequent values increase.

The enumeration associated numbers can be modified by assignment, and subsequent values will increase, but the previous values will not be changed:

enum Color {
    Red, // 0
    Blue = 2, // 2
    Green // 3
}

After manual assignment, it appears that the previous value is overwritten by the latter value, but TypeScript does not make an error:

enum Color {
    Red = 1, // 1
    Blue = 0, // 0
    Green // 1
}
console.log(Color['Red'] === 1); // true
console.log(Color['Green'] === 1); // true
console.log(Color[1] === 'Red'); // false
console.log(Color[1] === 'Green'); // true

String Enum

Enumeration values can also be set to strings, and when one of the values is assigned to a string, subsequent values must be assigned:

enum Color {
    Red, // 0
    Blue = 'Blue', // Blue
    Green = 'Green' // Green
}

Constant Enumeration

Constant enumeration can improve performance a little. It is better to use const to declare if the enumeration value does not change:

const enum Color {
    Red,
    Blue,
    Green
}
const red = Color.Red;
// Compiled to const red = 0 /* Red */;

Constant enumerations differ from ordinary enumerations in that they are deleted during compilation and cannot contain computational members.

const enum Color {
    Red,
    Blue,
    Green
}
const red = Color.Red;
// Compiled into
// var Color;
// (function (Color) {
//     Color[Color["Red"] = 0] = "Red";
//     Color[Color["Blue"] = 1] = "Blue";
//     Color[Color["Green"] = 2] = "Green";
// })(Color || (Color = {}));
// const red = Color.Red;

Calculated terms

There are two types of enumeration items: constant member and computed member.

The preceding examples all use constant terms, and the final terms are obtained by calculation.

enum Color {
    Red,
    Green,
    Blue = 'blue'.length
}

The value after the calculated item needs to be assigned manually, otherwise it will be wrong because the initial value can not be obtained.

When the following conditions are met, enumeration members are treated as constants:

  • There is no initialization function and the previous enumeration members are constants. In this case, the value of the current enumeration member is added to the value of the previous enumeration member by 1. But the first enumeration element is an exception. If it has no initialization method, its initial value is 0.
  • Enumeration members are initialized using constant enumeration expressions. Constant enumeration expressions are subsets of TypeScript expressions that can be evaluated during compilation. When an expression satisfies one of the following conditions, it is a constant enumeration expression:

    • Digital literal quantity
    • Reference to a previously defined constant enumeration member (which can be defined in different enumeration types) if the member is defined in the same enumeration type, you can use an unqualified name to refer to it
      Constant Enumeration Expressions with Brackets
    • + Application of -, ~unary operators to constant enumeration expressions
    • + The -,*,/,%,<<,>>>>>>>>, &, |, ^ binary operator, constant enumeration expression as one of its operation objects. If the constant enumeration expression evaluates to NaN or Infinity, an error will be reported at the compilation stage.

Enumeration members in all other cases are treated as values that need to be calculated.

External enumeration

External enumeration is an enumeration type defined using declare enum:

declare enum Directions {
    Up,
    Down,
    Left,
    Right
}
const directions = [
    Directions.Up,
    Directions.Down,
    Directions.Left,
    Directions.Right
];

// Compilation results
const directions = [
    Directions.Up,
    Directions.Down,
    Directions.Left,
    Directions.Right
];

The type defined by declare will only be used for compile-time checks, and the compiled results will be deleted.

External enumerations, like declaration statements, often appear in declaration files. It is possible to use declare and const at the same time:

declare const enum Directions {
    Up,
    Down,
    Left,
    Right
}

let directions = [
    Directions.Up,
    Directions.Down,
    Directions.Left,
    Directions.Right
];

// Compilation results
let directions = [0 /* Up */, 1 /* Down */, 2 /* Left */, 3 /* Right */];

Tags: Javascript TypeScript

Posted on Tue, 13 Aug 2019 20:34:37 -0700 by benjaminj88