TypeScript Enum Naming Conventions

In this tutorial, I will explain the intricacies of TypeScript enum naming conventions. By the end of this post, you’ll understand how to name your enums effectively, making your code more readable and maintainable.

Enums, short for enumerations, are a feature in TypeScript that allows you to define a set of named constants. These constants can make your code more readable and less error-prone by replacing magic numbers or strings with meaningful names. For example:

enum State {
    NewYork,
    California,
    Texas,
    Florida
}

const selectedState = State.California;
console.log(selectedState); // Output: 1
console.log(State[1]); // Output: "California"

Output:

Enum naming convention in TypeScript

In the example above, State is an enum with four possible values: NewYork, California, Texas, and Florida.

Check out: Comparing enum values in TypeScript

Why Use Enums?

Enums help in creating a set of distinct cases, making your code easier to understand and maintain. They also help in documenting intent clearly. For instance, if you have a function that accepts a state, using an enum can make it clear which states are valid inputs.

function getStateTaxRate(state: State): number {
    switch (state) {
        case State.NewYork:
            return 0.08875;
        case State.California:
            return 0.0725;
        case State.Texas:
            return 0.0625;
        case State.Florida:
            return 0.06;
        default:
            throw new Error("Invalid state");
    }
}

const taxRate = getStateTaxRate(State.Texas);
console.log(`Tax rate for Texas: ${taxRate}`); // Output: Tax rate for Texas: 0.0625

Output:

TypeScript Enums Naming Convention

Check out: Check if an Enum Contains a Value in TypeScript

Naming Conventions for Enums

One of the most critical aspects of using enums effectively is following a consistent naming convention. This ensures that anyone reading your code can understand it quickly.

  1. Enum Names Should Be PascalCase Enum names should follow the PascalCase convention. This means that the first letter of each word in the name should be capitalized. For example:
   enum TaxCategory {
       IncomeTax,
       SalesTax,
       PropertyTax
   }

Using PascalCase makes it clear that TaxCategory is a type, not a variable or a function.

  1. Enum Members Should Be ALL_CAPS The members of the enum should be in ALL_CAPS. This helps differentiate them from other variables and functions in your code. For example:
   enum DaysOfWeek {
       SUNDAY,
       MONDAY,
       TUESDAY,
       WEDNESDAY,
       THURSDAY,
       FRIDAY,
       SATURDAY
   }

This convention makes it clear that SUNDAY is a constant value within the DaysOfWeek enum.

  1. Use Singular Nouns for Enum Names Enum names should typically be singular nouns. This makes it clear that the enum represents a single concept or category. For example:
   enum VehicleType {
       CAR,
       TRUCK,
       MOTORCYCLE
   }

Using singular nouns helps maintain clarity and consistency across your codebase.

Check out: Get Enum Key by Value in TypeScript

Practical Examples

Let’s look at some practical examples to illustrate these conventions.

Example 1: Enum for U.S. States

Suppose we need to create an enum for U.S. states. We would follow the naming conventions outlined above:

enum USState {
    ALABAMA,
    ALASKA,
    ARIZONA,
    ARKANSAS,
    CALIFORNIA,
    COLORADO,
    CONNECTICUT,
    DELAWARE,
    FLORIDA,
    GEORGIA,
    HAWAII,
    IDAHO,
    ILLINOIS,
    INDIANA,
    IOWA,
    KANSAS,
    KENTUCKY,
    LOUISIANA,
    MAINE,
    MARYLAND,
    MASSACHUSETTS,
    MICHIGAN,
    MINNESOTA,
    MISSISSIPPI,
    MISSOURI,
    MONTANA,
    NEBRASKA,
    NEVADA,
    NEW_HAMPSHIRE,
    NEW_JERSEY,
    NEW_MEXICO,
    NEW_YORK,
    NORTH_CAROLINA,
    NORTH_DAKOTA,
    OHIO,
    OKLAHOMA,
    OREGON,
    PENNSYLVANIA,
    RHODE_ISLAND,
    SOUTH_CAROLINA,
    SOUTH_DAKOTA,
    TENNESSEE,
    TEXAS,
    UTAH,
    VERMONT,
    VIRGINIA,
    WASHINGTON,
    WEST_VIRGINIA,
    WISCONSIN,
    WYOMING
}

In this example, the enum name USState is in PascalCase, and the members are in ALL_CAPS.

Check out: TypeScript Enum Reverse Mapping

Example 2: Enum for Vehicle Types

Consider an application that deals with different types of vehicles. We can define an enum to represent these types:

enum VehicleType {
    CAR,
    TRUCK,
    MOTORCYCLE,
    BICYCLE,
    BOAT
}

Again, the enum name VehicleType is in PascalCase, and the members are in ALL_CAPS.

Common Pitfalls and How to Avoid Them

Even with the best intentions, it’s easy to make mistakes when naming enums. Here are some common pitfalls and how to avoid them:

  1. Using Inconsistent Naming Styles Mixing different naming styles can make your code confusing. Stick to a single naming convention for all your enums.
   // Avoid this
   enum vehicleType {
       Car,
       Truck,
       Motorcycle
   }

   // Use this
   enum VehicleType {
       CAR,
       TRUCK,
       MOTORCYCLE
   }
  1. Using Plural Nouns for Enum Names Using plural nouns can make it unclear whether the enum represents a single concept or multiple items.
   // Avoid this
   enum Vehicles {
       CAR,
       TRUCK,
       MOTORCYCLE
   }

   // Use this
   enum Vehicle {
       CAR,
       TRUCK,
       MOTORCYCLE
   }
  1. Using Generic or Non-Descriptive Names Generic names don’t convey the purpose of the enum. Use descriptive names that clearly indicate what the enum represents.
   // Avoid this
   enum Status {
       ONE,
       TWO,
       THREE
   }

   // Use this
   enum OrderStatus {
       PENDING,
       SHIPPED,
       DELIVERED
   }

Conclusion

Mastering TypeScript enum naming conventions is essential for writing clean, maintainable code. By following the guidelines outlined in this tutorial, you can ensure that your enums are named consistently and clearly, making your code easier to understand and work with.

Enums are a powerful feature in TypeScript, and using them effectively can significantly improve the quality of your code. Remember to use PascalCase for enum names, ALL_CAPS for enum members, and singular nouns for enum names. By adhering to these conventions, you’ll be well on your way to writing cleaner, more maintainable TypeScript code.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.