[SOLVED] Typescript enum values as array

Issue

Is it possible to get the values of an enum in TypeScript as an array?

Like this:

enum MyEnum {
    FOO = 'foo',
    BAR = 'bar'
}

becomes

['foo', 'bar']

Solution

Yes, it is possible to use:

Object.values(MyEnum)

because enum is an JS object after compilation:

var MyEnum;
(function (MyEnum) {
    MyEnum["FOO"] = "foo";
    MyEnum["BAR"] = "bar";
})(MyEnum || (MyEnum = {}));

Answered By – marsibarsi

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *