Mastering Enum in TypeScript: Enhance Readability and Type Safety

Mastering Enum in TypeScript: Enhance Readability and Type Safety

TypeScript, a superset of JavaScript, offers various advantages, including support for enum types. Enums in TypeScript are a helpful tool that allows developers to construct a set of named constants, allowing for a more expressive and type-safe approach to data representation. In this post, we will go into the realm of enum in TypeScript, covering its merits, and practical use cases.

Understanding enum in TypeScript: An enum is a data type that represents a set of named values. An enum in TypeScript allows developers to establish a group of related constants that may be used across their codebase. Enums are used mainly to improve code readability, self-documentation, and type-safe. Unlike in other programming languages, TypeScript enums can take string values, allowing greater flexibility in expressing various data types.

Defining Enum Types: To create an enum in TypeScript, you use the enum keyword followed by the name of the enum and a list of named constants. Here's a simple example of defining an enum representing days of the week:

enum DaysOfWeek {

In this example, we have defined an enum called DaysOfWeek with seven named constants representing the days of the week. By default, each constant is assigned a numeric value starting from 0. However, you can explicitly assign values to the enum members.

Assigning Values to Enum Members: By default, the first enum member is assigned a value of 0, and subsequent members are assigned incremented values. However, you can manually assign values to enum members. Take a look at this modified example:

enum DaysOfWeek {
  Sunday = 1,
  Monday = 2,
  Tuesday = 3,
  Wednesday = 4,
  Thursday = 5,
  Friday = 6,
  Saturday = 7,

In this case, we have assigned specific numeric values to each day of the week, starting from 1. It's important to note that enum member values can be either numbers or strings.

Using Enum Types: Once you have defined an enum, you can start using it in your code. Enum members can be accessed using the dot notation. Let's explore a few ways to utilize enums in TypeScript.

  1. Assigning enum values to variables:
let currentDay: DaysOfWeek = DaysOfWeek.Monday;

2.   Switch statements:

switch (currentDay) {
  case DaysOfWeek.Monday:
    console.log("It's Monday!");
  case DaysOfWeek.Tuesday:
    console.log("It's Tuesday!");
  // ...

3.   Enum iteration:

for (let day in DaysOfWeek) {

What are the benefits of using enum in typescript:

  1. Improved code readability: Enums provide meaningful names to constants, making the code more readable and self-explanatory.
  2. Type safety: TypeScript's strong typing system ensures that enum values are used correctly, reducing the chances of errors.
  3. Easy refactoring: When refactoring code, changing enum values automatically propagates throughout the codebase.
  4. Compile-time checks: TypeScript compiler can detect and warn about potential issues, such as using undefined enum values.

Tired of reading? Watch this video to see how Wojtek makes things quick and easy (available in Polish only).

Wyświetl ten post na Instagramie

Post udostępniony przez Ragnarson (@ragnarsoncom)