Enums In TypeScript

Introduction
 
Enums in TypeScript are named constants which are used to create unique classes. Enum is used to give a unique name to a constant and if we want to use it, we can access it via its name. 
 
Enum syntax
 
For using Enum, we need to use keyword enum and then we can provide a unique name for it.
  1. // Enum syntax  
  2.   
  3. enum <enum_constant>  
  4. {  
  5.      // enum values  
  6. }  
Primarily, there are two types of enums supported by TypeScript:
  • String-based enums
  • Numeric enums 
Prerequisites
 
If you are new to TypeScript and don't have it installed, then you need to download TypeScript first. Using npm, you can use the below command to download TypeScript and can use features of TypeScript. 
 
Open the console and execute the below command.
  1. npm install -g TypeScript  
After executing the above command, you can check what version of TypeScript you have installed. 
  1. tsc --version  
So, after that, you can see the message in the console as below.
 
 
 
Now, let's see both the types of enum with details. 
 
String-based Enums  
 
String-based enums are pretty easy to implement. They use collections of strings and every string value represents its own index. Let's see how string-based enums work. 
 
I'm going to create stringenum.ts file.  
  1. class StringEnum {  
  2.     public static main(): number {  
  3.         // String Enums  
  4.         enum Contribution  
  5.         {  
  6.             "Article" = "ARTICLES",  
  7.             "Blog" = "BLOG",  
  8.             "Video" = "VIDEO",  
  9.             "Resources" = "RESOURCES",  
  10.             "Interview Question" = "INTERVIEW QUESTION"  
  11.         }  
  12.         console.log(Contribution);  
  13.         return 0;  
  14.     }  
  15. }  
  16.   
  17. StringEnum.main();   
As you can see in the above code snippet. I created a simple class with main() method which is mandatory, after that I created an enum called Contribution and within that also provided different string values. 
 
Now, it's time to compile and see the list of contributions of Enum.
 
 
 
To select any specific item from enum, we always can do by providing a specific index like below.
  1. enum Contribution  
  2.        {  
  3.            "Article" = "ARTICLES",  
  4.            "Blog" = "BLOG",  
  5.            "Video" = "VIDEO",  
  6.            "Resources" = "RESOURCES",  
  7.            "Interview Question" = "INTERVIEW QUESTION"  
  8.        }  
  9.        console.log(Contribution["Interview Question"]);  
And you will get an output like this.
 
 
 
Numeric Enums 
 
By default, Enums are serialized; I mean to say that all items in enum are incremented values from 0 to the number of items 0 , 1 2 , ...... , N 
 
You can initialize any item of an enum with its own value and can see the difference. Check out the below snippet to create a numericenum.ts file:
  1. class NumericEnum {  
  2.     public static main(): number {  
  3.         // String Enums  
  4.         enum Contribution {  
  5.             "Article",  
  6.             "Blog",  
  7.             "Video",  
  8.             "Resources",  
  9.             "Interview Question"  
  10.         }  
  11.         console.log(Contribution);  
  12.         return 0;  
  13.     }  
  14. }  
  15.   
  16. NumericEnum.main();  
After executing the above snippet using node numericenum, you can see the tree of values with its index number.
 
 
 
As you can see in the above screen, values are serialized from 0 to 4; but if you want different incremental values, then you can provide a specific index like below. 
  1. enum Contribution {  
  2.            "Article",  
  3.            "Blog" = 10,  
  4.            "Video",  
  5.            "Resources" = 15,  
  6.            "Interview Question"  
  7.        }  
  8.        console.log(Contribution);  
I have provided different incremental values to Blog and Resources, respectively 10 and 15, and then you can see the difference.
 
 
 
Conclusion 
 
That's it for now. We have seen with examples what enum is, how to declare and use it, and what are its different types.
 
I hope you liked this. Thanks for reading.