您的位置:首页 > 其它

[Typescript] Typescript Enums vs Booleans when Handling State

2016-03-25 00:31 429 查看
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`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: