0

i want to get only the values of an enum as an array. So if you have:

    enum A {
        dog = 1,
        cat = 2,
        ant = 3
    }

What i want: ["dog", "cat", "ant"] by:

    Object.values(A)

but it gets you: ["dog", "cat", "ant", 0, 1, 2]

Also the values returned (0, 1, 2) would not match the given ones (1, 2, 3) anyway. How can i accomplish that?

Shawn Xiao
  • 560
  • 5
  • 18
endlacer
  • 135
  • 8

1 Answers1

0

The short answer you can't since typescript would compile your enum into the following

var A;
(function (A) {
    A[A["dog"] = 1] = "dog";
    A[A["cat"] = 2] = "cat";
    A[A["ant"] = 3] = "ant";
})(A || (A = {}));

link to typescript playground

however, since enums can't have numeric keys you can do the following to get the keys

Object.keys(A).filter(isNaN)

this works because an Enum can't contain a numeric value as an entry

Amr
  • 1
  • 2
  • 1