博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
typescript 枚举_TypeScript枚举声明和合并
阅读量:2504 次
发布时间:2019-05-11

本文共 2543 字,大约阅读时间需要 8 分钟。

typescript 枚举

This is the second post on declaration merging in TypeScript. In the , we looked at what declaration merging is and started with interfaces. In this post, we will look at how to merge enums.

这是有关TypeScript中声明合并的第二篇文章。 在上 ,我们研究了声明合并是什么,并从接口开始。 在本文中,我们将研究如何合并枚举。

For an introduction to Enums and why they are useful, you can .

有关Enums以及它们为什么有用的介绍,您可以 。

Let’s get started:

让我们开始吧:

enum Department {  IT,   Marketing}enum Department {  HR}

In our code above, since both enums have the same name, Department, TypeScript should be able to merge them, right? Well, not so fast! This will actually throw an error in the second enum declaration if we are to leave it like this. The error reads: In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element.

在上面的代码中,由于两个枚举都具有相同的名称Department ,所以TypeScript应该能够合并它们,对吗? 好吧,不是那么快! 如果我们要这样保留它,实际上将在第二个枚举声明中引发错误。 错误显示为: In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element.

What this means is that, enum elements are automatically assigned values if not specified. So IT in the first Department enum is assigned the value 0 and Marketing is assigned the value of 1. In the second Department, since HR is the first element in that enum, it is also assigned the value of 0. When merged, both IT and HR will have a value of 0 and that is not permitted hence, the error.

这意味着,如果未指定枚举元素,则会自动为其分配值。 因此,第一个Department枚举中的IT分配值为0Marketing分配值为1 。 在第二个Department ,由于HR是该枚举中的第一个元素,因此它也被赋值为0 。 合并后, ITHR的值均为0 ,因此不允许该错误。

To solve this, we can specify a value for the first element in our enum. Subsequent elements in the enum will have their values increased by one if not specified.

为了解决这个问题,我们可以为枚举中的第一个元素指定一个值。 如果未指定,则枚举中的后续元素的值将增加一。

enum Department {  IT = 1,   Marketing // has a value of 2, that is 1 + (IT value)}enum Department {  HR // has an automatically assigned value of 0}console.log(Department[1]) // ITconsole.log(Department[2]) // Marketingconsole.log(Department[0]) // HR

By specifying the value of 1 to IT, when HR is automatically assigned the value of 0, it won’t cause any error because no other element has that value.

通过为IT指定1的值,当HR自动分配0值时,不会引起任何错误,因为没有其他元素具有该值。

Of course, we can also assigned specific values to all the elements too.

当然,我们也可以为所有元素分配特定的值。

enum Department {  IT = 5,   Marketing = 3}enum Department {  HR = 8}console.log(Department[5]) // ITconsole.log(Department[3]) // Marketingconsole.log(Department[8]) // HR

That’s it. 😎😎

而已。 😎😎

翻译自:

typescript 枚举

转载地址:http://athgb.baihongyu.com/

你可能感兴趣的文章
MySQL innert join、left join、right join等理解
查看>>
sdc时序约束
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>