We have an enum
:
enum Values {
v1 = 'v1',
v2 = 'v2'
}
We want to get the values in v1
and v2
in the form string[]
.
Approach
enum Values {
v1 = 'v1',
v2 = 'v2'
}
let values:string[] = Object.keys(Values).map(key => Values[key]).filter(k => !(parseInt(k) >= 0)
console.log(values);
Demo
Typescript Design
Would be nice if Typescript gave us a utility for this. Something like:
const values:Array<T> = Enum.values<T>(enum);
This is the Typescript Feature Request.