Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

TypeScript enumerity


May 07, 2021 TypeScript



TypeScript enumerity

Using enumerals, we can define some numeric constants with names. Enumerants enum the enum keyword.

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

An enumerity type can contain zero or more enumerumered members. An enumeration member has a numeric value, which can be a constant or a calculated value When the following conditions are met, the enumeration member is treated as a constant:

  • There is no initialization function and the previous enumerated members are constants. I n this case, the value of the current enumeration member is the value of the last enumeration member plus 1. T he exception is the first enumeraled element. If it does not have an initialization method, its initial value 0
  • Enumerated members are initialized using constant enumeration expressions. C onstant enumeration expressions are a subset of TypeScript expressions that can be evaluated during the compilation phase. An expression is a constant enumeration expression when it meets one of the following criteria:
    • Digital literal
    • Reference to a previously defined constant enumeration member( which can be defined in a different enumeration type) If the member is defined in the same enumeration type, you can refer to it with an unqualified name.
    • Constant enumeration expressions with parentheses
    • + , - , ~ The ion operator is applied to the constant enumeration expression
    • + , - , * , / , % , << , >> , >>> , & , | , ^ Binary operator, constant enumeration expression as one of its operational objects If the constant enumeration NaN Infinity an error is reported during the compilation phase.

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

enum FileAccess {
    // constant members
    None,
    Read    = 1 << 1,
    Write   = 1 << 2,
    ReadWrite  = Read | Write
    // computed member
    G = "123".length
}

Enumerity is an object that really exists at runtime. One reason is that this allows reverse mapping from enumeration values to enumeration names.

enum Enum {
    A
}
let a = Enum.A;
let nameOfA = Enum[Enum.A]; // "A"

Compiled into:

var Enum;
(function (Enum) {
    Enum[Enum["A"] = 0] = "A";
})(Enum || (Enum = {}));
var a = Enum.A;
var nameOfA = Enum[Enum.A]; // "A"

In the generated code, the enumerate type is compiled into an object that contains a two-way map name -gt; value and value -gt; name R eference enumeration members always generate property access once and are never inline. I n most cases this is a good and correct solution. S ometimes, however, the demand is more stringent. W hen accessing enumeration values, you can use constant enumeration to avoid generating extra code and indirect references. Constant enumerants enum modifier before the const keyword.

const enum Enum {
    A = 1,
    B = A * 2
}

Constant enumerations can only use constant enumeration expressions and, unlike regular enumerations, are removed during the compilation phase. C onstant enumeraled members are inline where they are used. This is because constant enumeration cannot have calculated members.

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

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

The generated code is:

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

External enumerity

External enumerity is used to describe the shape of an enumerity type that already exists.

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

There is an important difference between external enumeration and non-external enumeration, in normal enumeration, members without initialization methods are treated as constant members. For a very large number of external enumerations, there is no initialization method that needs to be calculated.