TypeScript enum 枚举实现原理
TypeScript enum 枚举实现原理
反向映射
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,
}
*/

"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.

OK

computed enum const bug
remove
constkeyword
// 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,
}
refs
https://www.typescriptlang.org/docs/
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
TypeScript enum 枚举实现原理的更多相关文章
- Enum枚举法
java enum(枚举)使用详解 + 总结 enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中. 下面是我在使用 enum 过程 ...
- Enum 枚举类
目录 Enum 枚举类 基础 定义与用途 基本方法 示例 进阶 实现原理 枚举与Class对象 自定义枚举类和构造方法及toString() Enum中使用抽象方法来实现枚举实例的多态性 Enum与接 ...
- TypeScript入门七:TypeScript的枚举
关于枚举 数字枚举 字符串枚举 异构枚举 计算的和常量成员 运行时的枚举与反向映射 常量枚举与外部枚举 一.关于枚举 枚举:一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计 ...
- c# (ENUM)枚举组合类型的谷歌序列化Protobuf
c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...
- C#将Enum枚举映射到文本字符串
介绍 当将以前的C代码移植到C#中时,我快发疯了,因为有很多的数组需要将常量映射到字符串.当我在寻找一个C#的方法来完成的时候,我发现了一个自定义属性和映射的方法. 如何使用代码? 对每一个enum枚 ...
- MVC3不能正确识别JSON中的Enum枚举值
一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...
- 161208、Java enum 枚举还可以这么用
在大部分编程语言中,枚举类型都会是一种常用而又必不可少的数据类型,Java中当然也不会例外.然而,Java中的Enum枚举类型却有着许多你意想不到的用法,下面让我们一起来看看. 先来看一段代码示例: ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- java之enum枚举(2015年05月28日)
背景: 今天启动了一个新的项目,由于要从之前的旧项目中拿过来一些代码,所以就看了下公司之前项目代码,发现有定义的常量类,也有枚举类,然后就在想着两者的功能差不多,那他们之间到底有什么区别呢,所以就决定 ...
随机推荐
- Shell从入门到精通
熟悉基本shell操作不仅是运维的基本功,对于开发来说也是多多益善,我在学习的过程中,总结了十个练手的小demo,并附上涉及的知识点,仅供娱乐. 1. 多线程ping监控,检查同一网段的IP是否连通 ...
- vim 查找并替换多个匹配字符
通常我们在使用vim的使用需要查找文档中是否含有需要的字符 1.vim 1.txt进入文档编辑 2.输入/键,再输入需要查找的字符,或者输入?键再输入需要查找的字符 3.查找到后可以enter进去,再 ...
- 20201104gryz模拟赛解题报告
写在前面 \(Luckyblock\) 良心出题人, 题面好评 T1还是蛮简单的,用一个栈来维护就能过(某天听说 \(Luckyblock\) 出了套题,T1是个考栈的,看来就是这道了 注:栈的清空只 ...
- linux:搭建java web环境
介绍 运行java web的环境 搭建 准备 Linux:Linux 操作系统 Apache Tomcat:Web 应用服务器 JDK:Java 开发工具包 jdk的安装 1.下载 链接 2.上传服务 ...
- 转载,Pandas 数据统计用法
pandas模块为我们提供了非常多的描述性统计分析的指标函数,如总和.均值.最小值.最大值等,我们来具体看看这些函数: 1.随机生成三组数据import numpy as npimport panda ...
- Spring框架相关博文集
收藏一些干货博文. Spring 多数据源管理源码分析 Spring事务管理详解 Spring源码解析 Spring框架自学之路
- 数字转金额格式* 999999.99 TO 999,999.99
/** * 数字转金额格式 * 999999.99 TO 999,999.99 * @param d * @return */ public static String doubleToStr(dou ...
- 小白搭建WNMP详细教程---NGINX、MYSQL、PHP的整合配置
我自定义安装后的目录结构如下: 安装在D盘的WNMP下,其中WWW是网站的目录.ZIPS是放压缩包文件. 一.配置环境变量 在桌面右击我的电脑,选择属性,出现窗口后,按下图所示操作: 点击编辑后,会出 ...
- docker(12)使用Dockerfile创建jenkins+python3+pytest环境
前言 之前我们用docker手动安装了jenkins环境,在jenkins中又安装了python3环境和各种安装包,如果我们想要在其他3台机器上安装,又是重复操作,重复劳动,那会显得很low,这里可以 ...
- 2020 CCPC Wannafly Winter Camp Day1 C. 染色图
2020 CCPC Wannafly Winter Camp Day1 C. 染色图 定义一张无向图 G=⟨V,E⟩ 是 k 可染色的当且仅当存在函数 f:V↦{1,2,⋯,k} 满足对于 G 中的任 ...