TypeScript enum 枚举实现原理 All In One 反向映射 双向映射 computed enum



TypeScript enum 枚举实现原理 All In One

反向映射 / 双向映射

​https://www.typescriptlang.org/docs/handbook/enums.html​

enum Direction {
Up,
Down,
Left,
Right
}


TypeScript enum 枚举实现原理,反向映射

"use strict";
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 1] = "Up";
Direction[Direction["Down"] = 2] = "Down";
Direction[Direction["Left"] = 3] = "Left";
Direction[Direction["Right"] = 4] = "Right";
})(Direction || (Direction = {}));

const log = console.log;

log(`Direction =`, Direction)

/*
{
1: "Up", 2: "Down", 3: "Left", 4: "Right",
Up: 1, Down: 2, Left: 3, Right: 4,
}

*/


TypeScript enum 枚举实现原理 All In One_TypeScript

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-21
* @modified
*
* @description
* @augments
* @example
* @link
*
*/

const log = console.log;

function logString(msg: string): void {
log(`message =`, msg)
}

function logNumber(msg: number): void {
log(`message =`, msg)
}


// ???? number enum
enum DirectionNumber {
Up,
Down,
Left,
Right
}
// ???? number enum with change index
enum DirectionIndex {
Up = 3,
Down,
Left,
Right
}

// ???? string & number mixed enum
enum DirectionMixed {
Up = "up",
Down = 1,
Left,
Right
}

// ???? string enum
enum Level {
A = "perfect",
B = "good",
C = "bad",
}

// ???? const enum
const enum Roles {
Admin,
User,
Operator,
}

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

// TypeScript enum 枚举实现原理,反向映射
// Direction ={
// 1: "Up", 2: "Down", 3: "Left", 4: "Right",
// Up: 1, Down: 2, Left: 3, Right: 4,
// }

logString(Level.A);

logNumber(Direction.Down);

logNumber(Roles.Admin);
// console.log(0 /* Admin */);

"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-21
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
var log = console.log;
function logString(msg) {
log("message =", msg);
}
function logNumber(msg) {
log("message =", msg);
}
// ???? number enum
var DirectionNumber;
(function (DirectionNumber) {
DirectionNumber[DirectionNumber["Up"] = 0] = "Up";
DirectionNumber[DirectionNumber["Down"] = 1] = "Down";
DirectionNumber[DirectionNumber["Left"] = 2] = "Left";
DirectionNumber[DirectionNumber["Right"] = 3] = "Right";
})(DirectionNumber || (DirectionNumber = {}));
// ???? number enum with change index
var DirectionIndex;
(function (DirectionIndex) {
DirectionIndex[DirectionIndex["Up"] = 3] = "Up";
DirectionIndex[DirectionIndex["Down"] = 4] = "Down";
DirectionIndex[DirectionIndex["Left"] = 5] = "Left";
DirectionIndex[DirectionIndex["Right"] = 6] = "Right";
})(DirectionIndex || (DirectionIndex = {}));
// ???? string & number mixed enum
var DirectionMixed;
(function (DirectionMixed) {
DirectionMixed["Up"] = "up";
DirectionMixed[DirectionMixed["Down"] = 1] = "Down";
DirectionMixed[DirectionMixed["Left"] = 2] = "Left";
DirectionMixed[DirectionMixed["Right"] = 3] = "Right";
})(DirectionMixed || (DirectionMixed = {}));
// ???? string enum
var Level;
(function (Level) {
Level["A"] = "perfect";
Level["B"] = "good";
Level["C"] = "bad";
})(Level || (Level = {}));
//number enum
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 1] = "Up";
Direction[Direction["Down"] = 2] = "Down";
Direction[Direction["Left"] = 3] = "Left";
Direction[Direction["Right"] = 4] = "Right";
})(Direction || (Direction = {}));
// TypeScript enum 枚举实现原理,反向映射
// Direction ={
// 1: "Up", 2: "Down", 3: "Left", 4: "Right",
// Up: 1, Down: 2, Left: 3, Right: 4,
// }
logString(Level.A);
logNumber(Direction.Down);
logNumber(0 /* Admin */);


tsconfig bug ???

const timestamp = Date.now();
// const timestamp = new Date().getTime();
// ???? computed enum
const enum Dynamic {
role = Roles.User,
level = Level.A,
// time = Date.now(),
// time = new Date().getTime(),
time = timestamp,
// time = Math.random(),
value = 1 + 2,
len = "123".length,
}
// const enum member initializers can only contain literal values and other computed enum values.


TypeScript enum 枚举实现原理 All In One_const_02

OK

TypeScript enum 枚举实现原理 All In One_TypeScript_03

computed enum ❌ const bug

remove ​​const​​ keyword ✅


// ???? computed enum ❌ const bug
// const enum DynamicConstBug {
// role = Roles.User,
// level = Level.A,
// time = Date.now(),
// timestamp = new Date().getTime(),
// random = Math.random(),
// value = 1 + 2,
// len = "123".length,
// }
// const enum member initializers can only contain literal values and other computed enum values.

// ???? computed enum ✅
enum Dynamic {
role = Roles.User,
level = Level.A,
time = Date.now(),
timestamp = new Date().getTime(),
random = Math.random(),
value = 1 + 2,
len = "123".length,
}