[TypeScript] Collect Related Strings in a String Enum in TypeScript
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的更多相关文章
- strings.h 与 string.h 头文件的区别
今天使用 man string 来查看 string 文件的使用的方法(毕竟里面的函数名字和传入参数和发挥参数的类型,如果一段时间不使用,会产生遗忘.) 偶然发现,string.h 的man page ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- [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 ...
- [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 ...
- 深入浅出TypeScript(5)- 在React项目中使用TypeScript
前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...
- [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 ...
- [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 ...
- TypeScript enum 枚举实现原理
TypeScript enum 枚举实现原理 反向映射 https://www.typescriptlang.org/docs/handbook/enums.html enum Direction { ...
- enum和int、string的转换操作
enum Countries{ 中国 = 5, 美国, 俄罗斯, 英国, 法国} enum 和 int enum -> intint num = (int)Coun ...
随机推荐
- mac webstrom 安装less
1.检验电脑是否安装less lessc -v 2.如果没有执行全局安装命令 npm install -g less 3.webstrom -> Preferencs-> File Wat ...
- C# 获取目录下文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- autorun - 自动装载/卸载CDROMs并在装载后执行/path/to/cdrom/autorun
总览 autorun [-lmqv?V] [-a EXEC] [-c CDPLAYER] [-e STRING] [-i MILLISEC] [-n STRING] [-t STRING] [--au ...
- fabric的安装
https://blog.csdn.net/lepton126/article/details/79148027
- confluence的安装
参考链接:https://www.ilanni.com/?p=11989 一.什么是confluence confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实 ...
- php更改wampserver的站点目录
我都wampserver安装在f盘 F:\wamp\bin\apache\Apache2.4.4\conf文件夹下的hhtpd.conf文件ctrl+f查找DocumentRoot,第二次的位置修改即 ...
- 【02】HTML5与CSS3基础教程(第8版)(全)
[02]HTML5与CSS3基础教程(第8版)(全) 共392页. (魔芋:大体上扫了一遍.没有什么新东西,都是入门的一些基础知识.) 已看完. [美]elizabeth cast ...
- nyoj 86 找球号(一)(set,map)
找球号(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0& ...
- HDU-2817,同余定理+快速幂取模,水过~
A sequence of numbers Time Limit: 2000/1 ...
- POJ-20407Relatives/NYOJ-333mdd的烦恼,欧拉函数简单应用,模板A
poj Relatives Time Limit: 1000MS Memory Li ...