我正在尝试使用TypeScript。
假设我有一个像这样的object
let colors = {
RED: "r",
GREEN: "g",
BLUE: "b"
}
现在我想将这个内容转换成一个“枚举”类型。
enum Colors = {
RED = "r",
GREEN = "g",
BLUE = "b"
}
更新:
我需要对 colors 对象生成的 typings
进行修改。如果我往 colors 对象中添加了另一个 key
,那么它应该被包含在 typings
中。
如果我执行以下操作:
colors['YELLOW'] = "y"
那么生成的typings
应该是这样的
declare enum colors {
RED = "r",
GREEN = "g",
BLUE = "b",
YELLOW = "y"
}
相反,生成的类型是
declare const colors {
[x: string]: string
}
我该如何实现这个目标?
enum
转换为对象。 - Aleksey L.["red", "blue", "green"]
,我希望它被转换为enum Color { red, blue, green }
。 - Soldeplata Saketos