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. COGS.1200 ganggang的烦恼

    背景 Zhang Gangrui 年纪大了,记性不好,保险箱的密码记不住了,他只记得密码是一个数的阶乘各个位的数相加的和,最后还有个T或F,代表这个数是否为素数,正好,你到他家去了,他请你帮他这个忙, ...

  2. ALTER GROUP - 修改一个用户组

    SYNOPSIS ALTER GROUP groupname ADD USER username [, ... ] ALTER GROUP groupname DROP USER username [ ...

  3. vue之props传值与单向数据流

    (1)组件通信 父组件向子组件传递数据.这个正向传递数据的过程就是通过props来实现的. 两者区别:props中声明的数据与组件data函数return返回的数据的主要区别就是props来自父级,而 ...

  4. Vue+Bootstrap实现购物车程序(3)

    效果展示:(说明:使用webpack重构购物车程序,使用vue-cli生成项目脚手架) 文件结构: 代码: (1)将原来编写的btn-grp组件单独编写到BtnGrp.vue文件中 可以看到现在代码清 ...

  5. Python框架Django的入门

    本篇文章主要给大家介绍Django的入门知识:

  6. 2019年,Linux运维行业的趋势,跟不上学习就被淘汰

    运维行业经历了多年的发展,已经有了很大的变化,最开始的机房.网线.人肉,到现在一步步的自动化.智能化.容器化,运维人员的职业技能要求越来越高,稍不注意就可能被淘汰. 今天马小哥就来盘点一下2019年运 ...

  7. python Matplotlib 系列教程(三)——绘制直方图和条形图

    在本章节我们将学习如何绘制条形图和直方图 条形图与直方图的区别:首先,条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的 ...

  8. javascript脚本的延时加载

    javascript脚本的延时加载 向HTML页面中插入js代码的主要方法是使用<script>标签,在实际的开发中多采用外部文件的方式,主要考虑到外部js代码的可维护性及可缓存性等优点. ...

  9. PHP解惑(一)

    PHP给人的印象是入门简单的语言.当你的技术能力达到一定阶段时,会发现情况并非如此. PHP采用"极简主义",就是以入门容易为准则设计的,在十几年的持续发展历程中,它早已成为一个开 ...

  10. linux命令 iperf-网络性能测试工具

    博主推荐:更多网络测试相关命令关注 网络测试  收藏linux命令大全 iperf命令是一个网络性能测试工具.iperf可以测试TCP和UDP带宽质量.iperf可以测量最大TCP带宽,具有多种参数和 ...