Enum

1. What is Enum?

  • TypeScript's Enum is used to define a set of specific values

  • Enum is generally used instead of constant values, so Enum is widely used in TypeScript

  • Enum makes code more readable and prevents mistakes like typos

  • JavaScript doesn't natively support Enum, but TypeScript supports string and numeric Enums in the following form

    enum Color {
      // Define Color Enum
      // Enum has three values below
      Red,
      Green,
      Blue,
    }

2. Numeric Enum

  • Enum uses numeric as default value, and each value automatically starts from 0 and increments by 1

  • Values can also be manually specified as below

    enum Color {
      Red = 1,
      Green = 2,
      Blue = 4,
    }
  • Arithmetic operations can also be performed on Enum values

    enum Color {
      Red = 1,
      Green = 2,
      Blue = 4,
    }
    
    let c: Color = Color.Green;
    let greenValue: number = Color.Green;
    let blueValue: number = Color.Blue;
    
    console.log(c); // 2
    console.log(greenValue); // 2
    console.log(blueValue); // 4

3. String Enum

  • String Enum must initialize all Enum values with specific strings or other Enum values

  • String Enum doesn't have auto-incrementing unlike numeric Enum

  • When debugging, numeric Enum values may appear unclear, but string Enum always shows clear values making it easy to read

  • String-based Enum is mainly used to handle values brought from external sources in TypeScript (e.g., can define Enum representing HTTP request methods)

  • When specifying HTTP request methods, use Enum values like HttpMethod.Post

  • Using Enum this way can prevent mistakes like typos and improve code readability and stability

4. Reverse Mappings

  • Reverse mapping only exists in numeric Enum and is a characteristic that doesn't exist in string Enum

  • Can get value from Enum's key and & can also get key from value

Last updated