I wrote a function returning all values of a given enum as array. The implementation works, but I have a problem with the type of the return value.
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);
I get this error:
Error:(46, 7) TS2322: Type '(typeof Foo)[]' is not assignable to type 'Foo[]'. Type 'typeof Foo' is not assignable to type 'Foo'.
How can I change the signature of getEnumValues()
in order to return the type Foo[]
here?