Defining a conscribed set of constants with enum

TypeScript has a keyword enum that let you specify many possible values as a group where only a single item can be selected. Defining an enum can be done by providing potential keys that would automatically assign a sequential number from 0 to the first potential choice of the enum and so on:

  enum Weather {
Sunny,
Cloudy,
Rainy,
Snowy
}

It is possible to specify a value to a key to have fine-grained control. Any missing value will be the next sequence value. In the following code example, Sunny is set to 100, and Cloudy is automatically 101, Rainy is 102, and so on:

  enum Weather {
Sunny = 100,
Cloudy,
Rainy,
Snowy
}

It is possible to skip, in that case, you can only provide a bigger value and the value of the assigned one is sequential. In the following code example, the values are 100, 101, 200, and 201:

  enum Weather {
Sunny = 100,
Cloudy,
Rainy = 200,
Snowy
}

The enum can also support string or a mix of a string and a number:

enum Weather {
Sunny = "Sun",
Cloudy = "Cloud",
Rainy = 200,
Snowy
}

The enum can be accessed by the enum or by value. Accessing by the enum requires using the dot notation from the enum directly. The value returned is the enum. This is the common way to assign an enum in TypeScript. It is also possible to assign the value. The assignation by value is useful when data come from JSON. For example, the value is returned from an Ajax response. It bridges non-TypeScript into TypeScript:

let today: Weather = Weather.Cloudy;
let tomorrow: Weather = 200;

console.log("Today value", today); // Today value Cloud
console.log("Today key", Weather[today]); // Today key undefined
console.log("Tommorow value", tomorrow); // Tommorow value 200
console.log("Tommorow key", Weather[tomorrow]); // Tommorow key Rainy

In the previous code, accessing the value from the with the square bracket only works when the value in the square bracket is the type, not the value.

In addition to number and string, enum supports bitwise values with the help of a bit shift operator. It allows to check whether a value contains a single or an aggregate of value with an ampersand (&). The reason is that with the pipe | you can create a variable that contains several values. The stacking values can also reside inside the enum for reusability purposes but is not required:

  enum Weather {
Sunny = 0,
Cloudy = 1 << 0,
Rainy = 1 << 1,
Snowy = 1 << 2,
Stormy = Cloudy | Rainy // Can reside inside
}

let today: Weather= Weather.Snowy | Weather.Cloudy; // Can be outside as well

if (today & Weather.Rainy) { // Check
console.log("Bring an umbrella");
}

A value can hold more than a single value. This is useful if we want to persist the existing value intact and you need to use the sign |=. To remove a particular status, you need to use &= ~. Using these operators will swap the value at the right position in its binary format without affecting the remaining parts of the number:

today |= Weather.Rainy;
today &= ~Weather.Snowy;
console.log(today); // 3 -> 011 = Cloudy and Rainy

Finally, to check whether the variable is of a particular status, you must use the triple equals with the ampersand to the value you want to check against. The use of a single ampersand for comparison is a mistake. The ampersand returns a number, not a boolean. The comparison needs to be against the value that we want to check. It is possible to check against many values by creating a combined value in the comparison:

if (Weather.Rainy === (today & Weather.Rainy)) { // Check
console.log("Rainy");
}

if (Weather.Cloudy === (today & Weather.Cloudy)) { // Check
console.log("Cloudy");
}

if ((Weather.Cloudy & Weather.Rainy) === (today & Weather.Cloudy & Weather.Rainy)) { // Check
console.log("Cloudy and Rainy");
}

The enum is a great way to define a set of potential value from a specific domain for a variable. It has the convenience of being clear by naming the choices at your convenience and letting you decide the value of each entry if needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.105.190