DEV Community

Cover image for Enums in Typescript explained
ishan
ishan

Posted on

Enums in Typescript explained

In the world of programming enumerated type(enum) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type.

What is Enums

An Enum in TypeScript is a well-defined collection of a limited number of cases. That is, we write down all possibilities and do not allow anything else.

In typescript enums keyword offers a way for us to define a finite set of values — usually as named constants in a strongly typed way.

In short enums are useful when setting properties or values that can only be a certain number of possible values.

Enums basic

To create a basic enum in typescript we could start with the enum keyword. We can specify the enum members like

enum AvailableLaunguages {
  EN = 'English',
  FR = 'français',
  DN = 'danish',
  NP = 'Nepali',
};
Enter fullscreen mode Exit fullscreen mode

We have created a basic enum and named it AvailableLaunguages. To reference enum values, we would treat them as object attribute and access them using dot (.) notation.

console.log(AvailableLaunguages.EN); //English
console.log(AvailableLaunguages.FR); //français
console.log(AvailableLaunguages.DN); //Danish
console.log(AvailableLaunguages.NP); //Nepali
Enter fullscreen mode Exit fullscreen mode

There is also the possibility of not assigning values to the enums we described.

Lets take another example

enum GameLevel {
  Beginners,
  Medium,
  Noob,
  Pro,
  Worldclass
};
Enter fullscreen mode Exit fullscreen mode

By default, enums are numeric enums, i.e. GameLevel.Beginners is 0, GameLevel.Medium is 1, GameLevel.Noob is 2, GameLevel.Pro is 3, and GameLevel.Worldclass is 4.

Numeric enums are not strongly-typed to the values in the enum, but string enums are.

So, Enums are default to numbers if not assigned any values but it can also be assigned a string (or mix of both) if needed.

Using Enums as a datatype or Values

The flexibility of using enums are we could use it to store the data type of any enum defined or use it as a value.

const NewGamer : GameLevel = GameLevel.Beginners;
Enter fullscreen mode Exit fullscreen mode

Here we defined a value with a variable declaration using NewGamer and its data type is same we defined before which is the Gamelevel enum, added as a data type to our NewGamer variable.

Using Enums inside interfaces

Let's create a simple interface for a Gamer.


interface Gamer {
  id: number;
  name: string;
  number: number;
  level: GameLevel;
}

const player: Gamer = {
  id: 1,
  name: 'ishan',
  number: 0,
  level: GameLevel.Medium
}

console.log('player', player);

Enter fullscreen mode Exit fullscreen mode

As we can see we also stated the level of the gamer is the type of GameLevel.

We added a constraint to the value of level.

Enums are a type that can make code more readable and maintainable, where the meaning of the value of a variable is not apparent. This comes in handy when we are tasked with making comparisons between or among values from an enum, or even in their regular use.

TypeScript enums make it easier to document intent or to create a distinct set of cases.

https://www.typescriptlang.org/docs/handbook/enums.html

Thank you!

Top comments (0)