Geeks With Blogs
Phubar Baz Musings of an HTML5 Programmer
I just found an easy way to convert an enum value to its corresponding string value. Say you have the following enum.

enum Numbers { zero, one, two, three, four }

If you do you of course get the number 1. But let's say you have the enum value and you want to print it out. It's easy to get the string value by using an array indexer on the enum type.

var myNumber = Numbers.two;
var twoAsString = Numbers[myNumber]; // twoAsString == "two"

It seems a little strange but it works. If you look at the JavaScript you'll see how it's doing this. Click here to check it out on the TypeScript playground.

Posted on Monday, November 25, 2013 4:48 PM TypeScript , Programming , Enum | Back to top

Comments on this post: TypeScript Enums to String

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © PhubarBaz | Powered by: