Open In App

How enums works in TypeScript ?

In this article, we will try to understand all the facts which are associated with enums in TypeScript.

TypeScript enum: TypeScript enums allow us to define or declare a set of named constants i.e. a collection of related values which could either be in the form of a string or number or any other data type.



Syntax: Following is the syntax that we could use to initialize an enum in TypeScript-

enum enum_name {
    // values......
}

Reason to use an enum in TypeScript:



How does an enum work in TypeScript?

Numeric Enums: Numeric enums store string values as numbers and they can be declared using the keyword enum. Following are some of the examples which would help us to understand Numeric enums in a much better and clear way-

Example: In this example, we will be creating a numeric enum that will store Cars information and further we will display some specific results using that enum.




enum CarName {
    Honda,
    Toyota,
    Alto,
    Swift,
}
console.log(CarName);
console.log("Value of Alto is : "+ CarName.Alto);

Output:

{
  '0': 'Honda',
  '1': 'Toyota',
  '2': 'Alto',
  '3': 'Swift',
  Honda: 0,
  Toyota: 1,
  Alto: 2,
  Swift: 3
}
Value of Alto is : 2

Example: In this example, we will initialize one value inside an enum, so other values would be incremented on their own.




enum CarName {
    Honda = 10,
    Toyota,
    Alto,
    Swift,
}
console.log(CarName);
console.log("Value of Alto is : "+ CarName.Alto);

Output:

{
  '10': 'Honda',
  '11': 'Toyota',
  '12': 'Alto',
  '13': 'Swift',
  Honda: 10,
  Toyota: 11,
  Alto: 12,
  Swift: 13
}
Value of Alto is : 12

String Enums: String enums are quite similar to numeric enums, but their enum values are initialized with string values instead of numeric values. String enums have better readability than numeric enums.

The following example will help us to understand String Enums in a much better and clear manner-

Example: In this example, we will be creating a string enum that will store all the values in the string data type.




enum fruitsName {
    Apple = "APPLE",
    Banana = "Banana",
    Mango = "Mango",
    Papaya = "Papaya"
}
console.log(fruitsName);
 
console.log("Fruit name is : " + fruitsName.Apple);

Output:

{ Apple: 'APPLE', Banana: 'Banana', Mango: 'Mango', Papaya: 'Papaya' }
Fruit name is : APPLE

Heterogeneous Enums: Heterogeneous enums contain both numeric and string enums values. The following example will explain Heterogeneous enums in a much better and clear manner-

Example: In this example, we will be storing some data which is both in the form of a number as well as a string.




enum studentDetails {
    name = "ABCD",
    age = 20,
    rollno = 12345,
    address = "XYZ Place PQR city",
    school_name = "ABCDEFG"
}
console.log(studentDetails);

Output:

{
  '20': 'age',
  '12345': 'rollno',
  name: 'ABCD',
  age: 20,
  rollno: 12345,
  address: 'XYZ Place PQR city',
  school_name: 'ABCDEFG'
}

Place to use an enum: Enums should be used whenever there is a small set of fixed values available that are closely related and further these values are known at compile time.


Article Tags :