[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 ...
随机推荐
- selenium兼容非标准chrome内核的浏览器
多浏览器兼容性测试(1) RIDE已经支持多浏览器兼容性测试,例如: firefox ie chrome safari 但是,项目要求支持360极速和360安全浏览器.所以,我们需要增加代码让RIDE ...
- Laravel Excel安装及最简单使用
官网:https://docs.laravel-excel.com/ 1.安装 1.1.安装要求: PHP: ^7.0 Laravel: ^5.5 PhpSpreadsheet: ^1.6 ...
- webdrive脚本打开firefox浏览器,报“AttributeError: module 'selenium.webdriver' has no attribu
按照网上提供的方法: 下载geckodriver之后解压缩到 Firefox安装目录 下 添加 Firefox安装目录 到 系统变量Path 重启pycharm 照此步骤执行后,仍然报同样的错.折腾了 ...
- Timer时钟(之一)
using System.Timers; static void Main(string[] args) { ThreadingTimer(); DateTime a = DateTime.Now; ...
- $.fn.extend的用法
$.fn.extend({ sfMessages: function(m) { $("p.tips").html('<span class="circle ioc ...
- show()的方向
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- BZOJ 2039 人员雇佣 二元关系 最小割
题面太长了,请各位自行品尝—>人员雇佣 分析: 借用题解的描述: a.选择每个人有一个代价Ai b.如果有两个人同时选择就可以获得收益Ei,j c.如果一个人选择另一个不选会产生代价Ei,j 这 ...
- 装饰器(python)
一,装饰器定义:本质就是函数,功能是为其他函数添加新功能原则:1.不修改被装饰函数的源代码(开放封闭原则)2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式3.装饰器=高阶函数+函数嵌套+闭包高 ...
- LBE_登录Demo
目录 服务器 最小资产库创建 entity配置 实体的Python实现 创建第一个空间Space 让entity进入空间Space 客户端(unity) 生成客户端SDK 实现Client部分验证 验 ...
- 基于PHP的微信支付教程
微信支付作为各大移动支付方式之一,本课程只要向大家介绍并使用微信支付的常用功能,进而集合到已有的项目中去,希望各位能够快速上手并掌握实战"干货". 出处至:汇智网 hubwiz. ...