Tuesday, 26 August 2014

A [Description] Attribute approach for enums in Typescript

The problem at hand is that I need to use WebAPI services to get data structured in objects and use similar objects client-side in typescript code to structure and manipulate my frontend code. In C# and WebAPI I have the option to use enums:

1 public enum SearchOption
2 {
3 [EnumDescription("Starting With")]
4 StartingWith,
5 [EnumDescription("Contains")]
6 Contains,
7 [EnumDescription("Equal to")]
8 EqualTo,
9 [EnumDescription("Not Equal to")]
10 NotEqualTo
11 }

…which is very convenient because I then could get to the “description” info by reading the attribute for the enum. In typescript on the other hand, there are enums: