Typescript 在for循环中丢失枚举类型信息

Typescript 在for循环中丢失枚举类型信息,typescript,enums,Typescript,Enums,在下面的示例中,myColors的按键类型为Color。但是,在myColor对象上循环时,我很难保留该信息 enum Color { Red = 'RED', Green = 'GREEN', } type MyColors = { [C in Color]?: string; } const myColors: MyColors = { [Color.Red]: '#8B0000', } for (const [name, hex] of Object.entries(

在下面的示例中,
myColors
的按键类型为
Color
。但是,在
myColor
对象上循环时,我很难保留该信息

enum Color {
  Red = 'RED',
  Green = 'GREEN',
}

type MyColors = {
  [C in Color]?: string;
}

const myColors: MyColors = {
  [Color.Red]: '#8B0000',
}

for (const [name, hex] of Object.entries(myColors)) {
  // name is of type string here instead of Color
}
使用常规的
for in
循环或
Object.keys(myColors)
也会将枚举键转换为字符串


在对象的属性上循环时,有什么方法可以保留键的类型吗?如果不是,我如何断言循环中的
name
类型为
Color

TypeScript对
对象的定义。条目
是重载的,但两个重载都显式地使用字符串作为键类型。从
lib/lib.es2017.object.d.ts

然后它就起作用了:

for (const [name, hex] of entries(myColors)) {
  // type of `name` is `Color` now
}
进一步说,我发现如果我添加以下声明:

interface ObjectConstructor {
    /**
     * Returns an array of key/values of the enumerable properties of an object
     * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
     */
    entries<T>(o: T): [keyof T, T[keyof T]][];
}
(我在循环体中加入了
const n:Color=name;
,并与真正的编译器(不仅仅是操场)对此进行了双重检查。TypeScript在没有声明的情况下对此进行了抱怨,但有了声明,它就高兴了。)


但是,TypeScript问题列表中的一些问题让我认为定义可能会给您传递的其他类型的东西带来麻烦。因此,您可能希望使用单独的函数(最有可能的情况是,它将获得JIT)并在相关的地方使用它。

我已经更新了我的答案。如果您愿意添加声明,则不再需要帮助函数。您是一个救生员,谢谢!我想知道为什么
条目的官方定义中没有这一点
…@L.Berger-我很高兴,我至少学到了两件事。:-)我也知道这个定义。如果它使用了一个相对较新的功能,而之前的定义还没有更新以使用它,我也不会感到惊讶。@L.Berger-在TypeScript问题列表中搜索,上面的内容似乎会在某些其他情况下造成问题,因此您最好还是使用自己的
条目
实用程序功能(别担心,一个像样的JavaScript引擎会将其JIT化:-)。如果你想踏上征程,那么就开始吧,其中的大部分似乎都可以归结为。
for (const [name, hex] of entries(myColors)) {
  // type of `name` is `Color` now
}
interface ObjectConstructor {
    /**
     * Returns an array of key/values of the enumerable properties of an object
     * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
     */
    entries<T>(o: T): [keyof T, T[keyof T]][];
}
for (const [name, hex] of Object.entries(myColors)) {
  // type of `name` is `Color` now
}