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

本文共 765 字,大约阅读时间需要 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`

 

转载于:https://www.cnblogs.com/Answer1215/p/5317918.html

你可能感兴趣的文章
csv HTTP简单表服务器
查看>>
OO设计的接口分隔原则
查看>>
数据库连接字符串大全 (转载)
查看>>
java类加载和对象初始化
查看>>
iOS之Xcode自动管理证书、打包adhoc和testFlight测试安装
查看>>
UVA10976
查看>>
laravel实现第三方登录(qq登录)
查看>>
D - Mayor's posters - 2528(区间覆盖)
查看>>
使用ADO实现BLOB数据的存取 -- ADO开发实践之二
查看>>
回调--一个经典例子让你彻彻底底理解java回调机制
查看>>
React-redux及异步获取数据20分钟快速入门
查看>>
Node.js安装及环境配置之Windows篇
查看>>
Locust压力测试使用总结
查看>>
九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)
查看>>
历届试题 大臣的旅费
查看>>
优化SQL查询:如何写出高性能SQL语句
查看>>
迭代器-Iterator
查看>>
开学收好这 17 种工具 App,让你新学期学习更有效率
查看>>
html设定快捷键
查看>>
基于HTML5 audio元素播放声音jQuery小插件
查看>>