TypeScript枚舉的使用方法?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
class Color { // tricky:自增枚舉成員值 static counter = null // 枚舉成員 static Red = new Color('Red') static Green = new Color('Green') // 反向映射 static valueOf(value) { for (var name in Color) { if (!(name in Color.prototype) && Color[name].value === value) { return Color[name] } } } constructor(name, value){ if ('counter' in Color);else return this.name = name if (value == null) { if (Color.counter === null) { this.value = Color.counter = 0 } else { this.value = ++Color.counter } } else { this.value = Color.counter = value } } toString() { return `Color.${this.name}` } } delete Color.counter Object.freeze(Color) // tricky:禁止在定義之外的位置修改枚舉成員