当前位置:网站首页>Typescript -- Section 7 enumeration

Typescript -- Section 7 enumeration

2022-06-28 23:47:00 Run Coder

/* *  enumeration  enumeration(enum), enumeration (Enum) Type is used for scenarios where the value is limited to a certain range . *  Using enumerations, we can define some named constants .  Enumerations can be used to express intent clearly or to create a set of differentiated use cases . * TypeScript Supports numeric and string based enumeration . * * */

// Enumeration of numbers 
enum NumDirection {
    
    Up = 1,// Not initialized , The default from the 0 Start 
    Down,
    Left,
    Right
}
// Using enumerations is simple : Access enumeration members through enumerated properties , And the name of the enumeration to access the enumeration type 
console.log(NumDirection);// Access enum types by enum names 
console.log(NumDirection.Up);// Access enumeration members through enumerated properties 


//  String Enum 
// In string enumeration , Each member must use string literal , Or another string enumeration member to initialize .
enum EnumString{
    
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}
console.log(EnumString);// Access enum types by enum names 
console.log(EnumString.Up)// Access enumeration members through enumerated properties 


// Heterogeneous enumeration (Heterogeneous enums)
// It is not recommended to create enumerations of mixed data types   Examples are as follows :
enum BooleanLikeHeterogeneousEnum {
    
    No = 0,
    Yes = "YES",
}

// There are two types of enumerations : Constant term (constant member) And the calculated term (computed member)
// enumeration -- Constant 
//1. It is the first member of the enumeration and has no initializer , In this case, it is given a value  0:
// E.X is constant:
enum E {
     X }
//2. It has no initializer and its previous enumeration member is a   Digital constant .
//  In this case , The value of the current enumeration member is the value of its previous enumeration member plus 1.
// All enum members in 'E1' and 'E2' are constant.

enum E1 {
     X, Y, Z }

enum E2 {
    
    A = 1, B, C
}
/* * 3. Enumeration members use   Constant enumeration expression initialization . *  The constant enumeration expression is TypeScript Subsets of expressions , It can be evaluated at compile time .  When an expression satisfies one of the following conditions , It's just a constant enumeration expression : 0.  An enumeration expression literal ( Mainly string literal or numeric literal ) 1.  A reference to a previously defined constant enumeration member ( Can be defined in different enumeration types ) 2. Parenthesized constant enumeration expression  3. Unary operator  +, -, ~ One of them is applied to constant enumeration expressions  4. Constant enumeration expressions are used as binary operators  +, -, *, /, %, <<, >>, >>>, &, |, ^ Operation object of .  If the constant enumeration expression evaluates to  NaN or  Infinity, An error will be reported at the compilation stage . * * */
enum FileAccess {
    
    // constant members
    None,
    Read    = 1 << 1,// Shift left operator 
    Write   = 1 << 2,
    ReadWrite  = Read | Write,// An operator 
    // computed member
    G = "123".length,// Calculated item 
}

console.log(FileAccess.G)


// Union enum types and enum member types 
enum ShapeKind {
    
    Circle,
    Square,
}

interface Circle {
    
    kind: ShapeKind.Circle;
    radius: number;
}

interface Square {
    
    kind: ShapeKind.Square;
    sideLength: number;
}

let c: Circle = {
    
    kind: ShapeKind.Circle,
    // kind: ShapeKind.Square,// Report errors 
    // ~~~~~~~~~~~~~~~~ Error!
    radius: 100,
};
console.log(c.kind);


// Runtime enumeration 
enum E3 {
    
    X, Y, Z
}
function f(obj: {
     X: number }) {
    
    return obj.X;
}
// Works, since 'E' has a property named 'X' which is a number.
console.log(f(E3));


// Reverse mapping 
enum Enum {
    
    A
}
let a = Enum.A;
let nameOfA = Enum[a]; // "A"
console.log(Enum.A);//0
console.log(nameOfA);//"A"


/* * const  Constant enumeration  *  Constant enumeration by using  const To define  *  The difference between constant enumeration and ordinary enumeration is , It will be removed during the compilation phase , And can't contain calculated members . * */
const enum EnumConst {
    
    A = 1,
    // B= 'abc'.length // Constant enumerations are not allowed to contain calculated members .
}


// External enumeration 
//  You must use const,  Otherwise declare  The defined type will only be used for compile time checks , The compilation result will be deleted 
declare const enum Directions {
    
    Up,
    Down,
    Left,
    Right
}
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
console.log(directions)



>  Wu Zhe : Common heart , Common heart !--《 Soldier assault 》

原网站

版权声明
本文为[Run Coder]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/179/202206282343284664.html