Thursday, May 15, 2014

Why Enums? Example in TypeScript

it's been a while since my last post, this it not because I don't have anything to write about! I have just been busy. since my last post I have created a new site called TypeScript.today this is a weekly compiled neews of whats going on in the TypeScript world, so if you have anything to share, please give me a note.


In this post I going through what an Enum is designed to solve, and then how to writing a simple Enum. I also going to show the difference between constant and computed member types.

Why Enums Sometimes as programmers we want to express the idea that a variable will be used for a specific purpose and should only be able to have a small number of values--for instance, a variable that stores the current direction of the wind might only need to store values corresponding to north, south, east, and west. One solution to this problem might be to use a number and some define'd values:

 var NORTH_WIND: number = 0;  
 var SOUTH_WIND: number = 1;  
 var EAST_WIND: number = 2;  
 var WEST_WIND: number = 3;  
 var NO_WIND: number = 4;  
 var wind_direction: number = NO_WIND;  


The problem with this approach is that it doesn't really prevent someone from assigning a nonsensical value to wind_direction; for instance, I could set wind_direction to -1 without any complaints from my compiler.
 
var NORTH_WIND: number = 0;
var SOUTH_WIND: number = 1;
var EAST_WIND: number = 2;
var WEST_WIND: number = 3;
var NO_WIND: number = 4;

var wind_direction: number = -1;


And if I looked at the type of wind_direction, i would see that it's just an integer, and this can be hard to see that something is wrong.

So the idea behind enumerated types is to create new data types that can take on only a restricted range of values. These values are all expressed as constants or computed types

Constant members declared with an integer value is assigned that value. A constant member declared without an integer value is assigned the value of the preceding constant member plus one, or the value zero if it is the first member in the enum body. The values of constant members are computed at compile-time and may be substituted for references to the members in the generated JavaScript code.
So here is an example of a enum with constant member types.
 
enum Windirections{
North = 0,
South = 1,
West = 2,
East = 3,
none = 4
}


Enums in TypeScript also support making computed members Expressions, specified for computed members must produce values of type Any, the Number primitive type, or an enum type. The values of computed members are not known at compile-time and no substitution is performed for references to computed members. heres is an example of a computed member called SoutEast where I assign it the values of South and East.

 
enum Windirections{
North = 0,
South = 1,
West = 2,
East = 3,
none = 4,
SoutEast = South + East
}
Conclusion We’ve seen how enums in the TypeScript language can be used to allow to constrain the values a variable takes on. I shown how to make constant and computed enum members. Enums can make your program more readable by eliminating magic numbers.