[Typescript] Typescript Enums vs Booleans when Handling State
vstypescript when State Handling
2023-09-14 09:00:54 时间
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 usage var card = CardSuit.Clubs; // Safety card = "not a member of card suit"; // Error : string is not assignable to type `CardSuit`
相关文章
- 使用体验神似VS Code?三步带你了解华为云CloudIDE前世今生
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- c# vs 中如何修改类模板
- vs中如何统计整个项目的代码行数
- Product Management vs. Product Marketing
- [RxJS] ShareReplay vs share
- 为VS code中的项目添加特定的智能提示功能
- igbinary vs serialize vs json_encode
- VS 对话框控件的Tab顺序问题
- Atitit 微服务实践 艾提拉著 微服务主要解决几个问题负载均很 目录 1. 微服务的模式 http请求层 vs服务层1 1.1. Http vs 服务层优缺点1 2. 实现技术2
- QT + VS 创建OCC项目
- vs 编译时不断出现该文件已在源编辑器之外被修改弹窗
- 墨刀 vs Axure RP
- VS 多进程调试(C#和C )
- VS code创建虚拟环境并安装Python包
- VS配置matplotlib库边界可视化步骤:
- 华为数据中心VS技术理论讲解