As of TypeScript 2.4, it is now possible to define string enums, or more precisely, enums with string members. Just like any other numeric enum, string enums can be made constant using the const modifier so that they disappear entirely from the generated JavaScript; in this case, all enum values will be inlined into the output.

For example,we  have the code:

fetch("https://swapi.co/api/people/1/", {
headers: {
Accept: 'application/json'
}
})
.then((res) => res.json())
.then(response => {
console.log(response.name)
});

We want to replace 'application/json' to use Typescript enum.

enum MediaTypes {
JSON = "application/json"
} fetch("https://swapi.co/api/people/1/", {
headers: {
Accept: MediaTypes.JSON
}
})
.then((res) => res.json())
.then(response => {
console.log(response.name)
});

From the compiled code, we can see the output:

var MediaTypes;
(function (MediaTypes) {
MediaTypes["JSON"] = "application/json";
})(MediaTypes || (MediaTypes = {}));
fetch("https://swapi.co/api/people/1/", {
headers: {
Accept: MediaTypes.JSON
}
})
.then(function (res) { return res.json(); })
.then(function (response) {
console.log(response.name);
});

The compile code, it add a IIFE define and set JSON code to 'application/json'.

Sometime, we don't want this meta code goes into production code, the way to do this is add "const":

const enum MediaTypes {
JSON = "application/json"
}
/*compiled code**/ fetch("https://swapi.co/api/people/1/", {
headers: {
Accept: "application/json" /* JSON */
}
})
.then(function (res) { return res.json(); })
.then(function (response) {
console.log(response.name);
});

As we can see, the output code doesn't have IIFE anymore, the code is much smaller.

You can get reverse mapping by using number:

enum Port {
NUM =
} /**compiled code*/
(function (Port) {
Port[Port["NUM"] = ] = "NUM";
})(Port || (Port = {}));

Last thing, if you really want to use "const" keyword but still want to keep IIFE meta code, you can set up in tsconfig.json:

{
"preserveConstEnums": true
}

[TypeScript] Collect Related Strings in a String Enum in TypeScript的更多相关文章

  1. strings.h 与 string.h 头文件的区别

    今天使用 man string 来查看 string 文件的使用的方法(毕竟里面的函数名字和传入参数和发挥参数的类型,如果一段时间不使用,会产生遗忘.) 偶然发现,string.h 的man page ...

  2. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  3. [TypeScript] Query Properties with keyof and Lookup Types in TypeScript

    The keyof operator produces a union type of all known, public property names of a given type. You ca ...

  4. [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS

    Using Object Oriented Programming, OOP, style allows us to apply Inversion of Control, IoC, and more ...

  5. 深入浅出TypeScript(5)- 在React项目中使用TypeScript

    前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...

  6. [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript

    Conditional types take generics one step further and allow you to test for a specific condition, bas ...

  7. [TypeScript] Find the repeated item in an array using TypeScript

    Say you have an array that has at least one item repeated. How would you find the repeated item. Thi ...

  8. TypeScript enum 枚举实现原理

    TypeScript enum 枚举实现原理 反向映射 https://www.typescriptlang.org/docs/handbook/enums.html enum Direction { ...

  9. enum和int、string的转换操作

    enum Countries{    中国 = 5,    美国,    俄罗斯,    英国,    法国} enum 和 int enum -> intint num = (int)Coun ...

随机推荐

  1. 虚拟机centOs Linux与Windows之间的文件传输

    一.配置环境 虚拟机Linux:Fedora 9 文件传输工具:SSHSecureShellClient-3.2.9 二.实现步骤 1. 在Windows中安装文件传输工具SSHSecureShell ...

  2. HDU_1856_带权并查集

    有10000000个同学,他们之间可能是直接朋友或者间接朋友,问最大的朋友圈有多少人. 一直觉得10000000的数组开不了,用map优化了一下,结果能开,且10000000也不会超时... #inc ...

  3. mathAge.call(btn) 函数call 改变函数内 this #js

    mathAge.call(btn) 函数call 改变函数内 this

  4. Less简介及安装

    CSS的短板 作为前端学习者的我们 或多或少都要学些 CSS ,它作为前端开发的三大基石之一,时刻引领着 Web 的发展潮向. 而 CSS 作为一门标记性语言,可能 给初学者第一印象 就是简单易懂,毫 ...

  5. JavaSE-24 多线程

    学习要点 线程概述 Java中的多线程 线程状态 线程调度 线程同步 线程间通信 线程概述 1  进程 进程就是应用程序的执行实例. 进程特征: 动态性:动态产生,动态消亡.进程启动,系统为其分配资源 ...

  6. Perl语言入门:第六章习题:处理用户所指定的名字并汇报相应的姓。

    37 print "\n----------------------------------_exercise_6_1--------------------------\n";  ...

  7. rsync 同步多台服务器项目目录

    server1:192.168.10.1server2:192.168.10.2实现server2实时同步server1的数据! ###############server1############# ...

  8. 诊断:ORA-00376 & ORA-01110

    现象: Errors in file /path/of/diag/rdbms/prod/PROD/trace/PROD_ora_13447.trc: ORA-00376: 此时无法读取文件 61 OR ...

  9. mysql通配符进行模糊查询

    在mysql数据库中,当我们需要模糊查询的时候 ,我们会使用到通配符. 首先我们来了解一下2个概念,一个是操作符,一个是通配符. 操作符 like就是SQL语句中的操作符,它的作用是指示在SQL语句后 ...

  10. 常用的四种设计模式 PHP代码

    // 工厂模式 interface Iuser { public function getUserName(); } class UserFactory { static public functio ...