【发布时间】:2018-10-18 00:41:16
【问题描述】:
我编写了一个函数,将给定枚举的所有值作为数组返回。实现是可行的,但是我对返回值的类型有疑问。
enum Foo {
FOO_1 = "FOO_1",
FOO_2 = "FOO_2",
}
function getEnumValues<T>(e:T): T[] {
let keys: string[] = Object.keys(e);
keys = keys.filter(key => e[key] !== undefined);
return keys.map(key => e[key]);
}
const fooValues:Foo[] = getEnumValues(Foo);
我收到此错误:
错误:(46, 7) TS2322: Type '(typeof Foo)[]' is notassignable to type 'Foo []'。类型 'typeof Foo' 不能分配给类型 'Foo'。
如何更改getEnumValues() 的签名以便在此处返回Foo[] 类型?
【问题讨论】:
标签: typescript generics enums type-inference