enums and _map property to get source string?

Topics: General
Nov 27, 2012 at 2:38 AM

Figured out that one can lookup the source text of an enum value by accessing the _map property. This is a hack though, what is the preferred way to do it?

enum MyEnum {

A,

B

}

var x = MyEnum.B; // x == 1

var src = MyEnum['_map'][x]; // src == "B"

Coordinator
Nov 27, 2012 at 10:04 PM

You got it.  The _map field gives you a way to get back to the names of the enums.

Nov 27, 2012 at 10:27 PM
Feels like a hack to use _map though. Any plans for an API that would be less implementation specific?
From: [email removed]
Sent: Tuesday, November 27, 2012 2:04 PM
To: [email removed]
Subject: Re: enums and _map property to get source string? [typescript:404673]

From: jonturner

You got it. The _map field gives you a way to get back to the names of the enums.

Coordinator
Nov 28, 2012 at 3:15 PM

Potentially.  In a sense, it's more of a convenience which matches closely to how you'd do it in JS.  That said, you could try submitting a feature request and see if there's enough support for people who want to print out enums more cleanly.