博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Typescript] Typescript Enums vs Booleans when Handling State
阅读量:7018 次
发布时间:2019-06-28

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

Handling state with Typescript enums, instead of booleans, is preferred because:

- Enums are more readable
- Enums can have as many states as you need while booleans only have 2
- You only need to keep track of state with 1 variable when using enums

 

TypeScript enums are number based. This means that numbers can be assigned to an instance of the enum, and so can anything else that is compatible with number.

enum Color {    Red,    Green,    Blue}var col = Color.Red;col = 0; // Effectively same as Color.Red

 

enum CardSuit {    Clubs,    Diamonds,    Hearts,    Spades}// Sample usagevar card = CardSuit.Clubs;// Safetycard = "not a member of card suit"; // Error : string is not assignable to type `CardSuit`

 

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

你可能感兴趣的文章
【很好的学习Linux驱动的教材】Linux那些事儿系列[全][pdf]
查看>>
Windows 2008 / 2008 R2 开启显卡硬件加速
查看>>
PHP配置文件详解php.ini
查看>>
浅谈OA系统与Portal门户的区别
查看>>
初学Redis(2)——用Redis作为Mysql数据库的缓存
查看>>
dva 脚手架目录分析
查看>>
《良质!PHP企业级系统开发》- PDF抢鲜版,附全书高清脑图
查看>>
docker create your own image
查看>>
sql 1130 is not allowed to connect to this MySQL
查看>>
设置IP地址、网关DNS
查看>>
WebSocket详解(三):深入WebSocket通信协议细节
查看>>
Java经典设计模式-结构型模式-桥接模式(Bridge)
查看>>
linux下使用 tar 命令
查看>>
数据库的OLTP和OLAP区别
查看>>
Java读取Properties文件的六种方法
查看>>
Delphi 与 C/C++ 数据类型对照表
查看>>
《.Net 基础系列》- 正则
查看>>
VC++6.0 连接MySQL
查看>>
poj 神奇的口袋
查看>>
Springboot start-** pom配置 以及xml配置 学习(三)
查看>>