问题描述
我需要构建一个可以发送枚举的函数:
I need to build a function where I can send an enum:
enum myExample {
value1,
value2
}
并返回一个类似
[myExample.value1,myExample.value2] : myExample[]
我这样做了,但是函数的返回类型是错误的 let x:(typeof myExample)[]
.
I did this, the but the return type of the function is wrong let x: (typeof myExample)[]
.
enum myExample {
value1,
value2
}
function getArrayWithNumberBaseEnumItems<T>(numberEnum: T): T[]{
let arrayWithEnumItems: T[] = [];
for (let item in numberEnum) {
if (isNaN(Number(item))) {
arrayWithEnumItems.push(numberEnum[item] as any);
console.log(numberEnum[item]);
}
}
return arrayWithEnumItems;
}
let x = getArrayWithNumberBaseEnumItems(myExample);
console.dir(x); // [0,1]
我正在使用Typescript 2.6
I am using Typescript 2.6
推荐答案
您可能会将命名 type myExample
与命名 value 混淆 myExample
.尽管名称相同,但它们并不相同. type myExample
是数字的枚举值的类型. value myExample
的类型为 typeof myExample
,这是键 value1
和 value2
的映射这些 myExample
枚举值.也就是说, typeofmyExample
类似于 {example1:myExample.example1,example2:myExample.example2}
.
You are likely confusing the named type myExample
with the named value myExample
. They are not the same, despite the same name. The type myExample
is the type of the enum values which are numbers. The value myExample
has type typeof myExample
, a map from keys value1
and value2
to those myExample
enum values. That is, typeof myExample
is something like {example1: myExample.example1, example2: myExample.example2}
.
(有关我在TypeScript中命名值和命名类型之间区别的更多抱怨,请参见此答案)
(For more of my ranting about the difference between named values and named types in TypeScript, see this answer)
因此,当您将值 myExample
传递给 getArrayWithNumberBaseEnumItems()
时,您正在传递 typeof myExample
并想要 myExample []
出来.从前者到后者的方法是进行查找从 T
(对应于 typeof myExample
)到 T [keyof T]
(表示" T
的属性值").
Therefore when you pass in the value myExample
to getArrayWithNumberBaseEnumItems()
, you are passing in typeof myExample
and want myExample[]
to come out. The way to get from the former to the latter is to do a lookup from T
(corresponding to typeof myExample
) to T[keyof T]
(meaning "the values of the properties of T
").
因此,您应该将类型修改为以下内容:
Thus you should amend your types to the following:
function getArrayWithNumberBaseEnumItems<T>(numberEnum: T): T[keyof T][] {
let arrayWithEnumItems: T[keyof T][] = [];
for (let item in numberEnum) {
if (isNaN(Number(item))) {
arrayWithEnumItems.push(numberEnum[item]); // no type assertion here
console.log(numberEnum[item]);
}
}
return arrayWithEnumItems;
}
请注意,我如何将 numberEnum [item]
的类型断言删除为 any
.您抑制的错误:
Note how I've removed your type assertion of numberEnum[item]
to any
. The error you were suppressing:
// Argument of type 'T[Extract<keyof T, string>]'
// is not assignable to parameter of type 'T'.
(TS3.1给出了错误.TS2.6可能给出了外观不同但相似的错误)
(that's the error TS3.1 gives. TS2.6 probably gives a different-looking but similar error)
试图告诉您,您试图将属性值推入键值映射数组中,这是一个错误.有时您需要断言才能完成工作,但这不是那些时候之一.
was trying to tell you that you were trying to push a property value onto an array of key-value maps, which was an error. Sometimes you need to assert to get things done, but this is not one of those times.
好的,希望能有所帮助.祝你好运!
Okay, hope that helps. Good luck!
这篇关于如何解析泛型函数中的枚举以返回枚举值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!