Skip to content
DeveloperMemos

Using TypeScript Enums

TypeScript, Enums2 min read

Enums are a powerful feature in TypeScript that allows you to define a set of named constants. They provide a way to organize related values, making your code more readable and maintainable. Enums in TypeScript are similar to enums in other programming languages such as Kotlin or Swift. In this article, we will focus on TypeScript enums and demonstrate their usage with some examples.

What are Enums?

An enum, short for enumeration, is a way to define a collection of related values that can be represented as named constants. Enums in TypeScript provide a convenient way to work with a set of values that have a clear relationship or specific meaning. By using enums, you can define a set of options or states that a variable can take, making your code more expressive and self-documenting.

Defining Enums

To define an enum in TypeScript, you can use the enum keyword followed by the enum name and a set of values enclosed in curly braces. Each value is assigned a unique numeric identifier automatically by default, starting from 0 and incrementing by 1 for each subsequent value. Here's an example:

1enum Color {
2 Red,
3 Green,
4 Blue,
5}

In the above example, we have defined an enum called Color with three values: Red, Green, and Blue. The default numeric identifiers for these values are 0, 1, and 2 respectively. You can access these values by using the dot notation, such as Color.Red, Color.Green, and Color.Blue.

Using Enums

Enums can be used in various ways to represent different scenarios. Let's look at a few examples to see how enums can be helpful in your TypeScript projects.

Example 1: Days of the Week

1enum DayOfWeek {
2 Sunday,
3 Monday,
4 Tuesday,
5 Wednesday,
6 Thursday,
7 Friday,
8 Saturday,
9}
10
11function getDayMessage(day: DayOfWeek): string {
12 switch (day) {
13 case DayOfWeek.Sunday:
14 return "It's Sunday!";
15 case DayOfWeek.Monday:
16 return "Hello, Monday!";
17 case DayOfWeek.Tuesday:
18 return "Happy Tuesday!";
19 // ...
20 }
21}
22
23console.log(getDayMessage(DayOfWeek.Monday)); // Output: Hello, Monday!

In this example, we define an enum called DayOfWeek representing the days of the week. The getDayMessage function takes a parameter of type DayOfWeek and returns a corresponding message based on the day. By using the enum, we ensure that only valid values (days of the week) can be passed to the function.

Example 2: HTTP Status Codes

1enum HttpStatusCode {
2 OK = 200,
3 BadRequest = 400,
4 Unauthorized = 401,
5 NotFound = 404,
6}
7
8function getMessageForStatusCode(code: HttpStatusCode): string {
9 switch (code) {
10 case HttpStatusCode.OK:
11 return "Success";
12 case HttpStatusCode.BadRequest:
13 return "Bad Request";
14 case HttpStatusCode.Unauthorized:
15 return "Unauthorized";
16 // ...
17 }
18}
19
20console.log(getMessageForStatusCode(HttpStatusCode.NotFound)); // Output: Not Found

In this example, we define an enum called HttpStatusCode representing common HTTP status codes. The `getMessage

ForStatusCodefunction takes a parameter of typeHttpStatusCode` and returns a corresponding message based on the status code. By using the enum, we ensure that only valid status codes can be passed to the function.

Custom Enum Values

Enums in TypeScript also allow you to specify custom values for each enum member. You can assign string or numeric values explicitly to the enum members. Here's an example:

1enum Planet {
2 Mercury = 1,
3 Venus = 2,
4 Earth = 3,
5 Mars = 4,
6}
7
8console.log(Planet.Venus); // Output: 2

In this example, we assign custom numeric values to each enum member of the Planet enum. The value of Planet.Venus is 2.

In Closing

Enums are a valuable feature in TypeScript that can improve the readability and maintainability of your code. They allow you to define a set of named constants, making it easier to work with related values and ensure type safety. By leveraging enums, you can make your code more expressive and self-explanatory!