我在应用程序之外的某处生成了以下枚举:
enum Colors {
Red = "RED",
Green = "GREEN",
Blue = "BLUE",
}
此枚举用作生成的接口属性的类型
interface MyObject {
objectName: string,
objectColor: Colors
}
是否可以覆盖枚举字符串值,以便相同的枚举值可以具有如下所示的新描述:
enum Colors {
Red = "RED car",
Green = "GREEN grass",
Blue = "BLUE sky",
}
答案 0 :(得分:0)
虽然你可以做到这一点,但我会提醒你不要这样做,如果你不早点做,一些变量可能会被旧值所困,并产生问题:
enum Colors {
Red = "RED",
Green = "GREEN",
Blue = "BLUE",
}
console.log(Colors.Red);
let red = Colors.Red;
(Colors as any)['Red'] = "Dark Red" // Force the enum to any and change the internal value.
console.log(Colors.Red);
console.log(red === Colors.Red); // false, because red got the old value
在运行时,enum
只是一个Javascript对象,其中包含具有指定值的enum
字段,因此更改它不是问题:
var Colors;
(function (Colors) {
Colors["Red"] = "RED";
Colors["Green"] = "GREEN";
Colors["Blue"] = "BLUE";
})(Colors || (Colors = {}));