Thursday, July 1, 2021

TypeScript Data Type - Enum | Example | Demo

TypeScript Data Type - Enum


Enums or enumerations are a new data type supported in TypeScript. Most object-oriented languages like Java and C# use enums. This is now available in TypeScript too.
In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric or string values.
There are three types of enums:
1. Numeric enum
2. String enum
3 Heterogeneous enum

1. Numeric Enum

Numeric enums are number-based enums i.e. they store string values as numbers. Enums can be defined using the keyword enum. Let's say we want to store a set of print media types. The corresponding enum in TypeScript would be:
2 String Enum
String enums are similar to numeric enums, except that the enum values are initialized with string values rather than numeric values.
The benefits of using string enums is that string enums offer better readability. If we were to debug a program, it is easier to read string values rather than numeric values.
3 Heterogeneous Enum
Heterogeneous enums are enums that contain both string and numeric values.

Example



Demo

Demo

No comments:

Post a Comment

How to build an Express and Node.js app with Typescript

  In this tutorial, you will learn how to set up a Node.js and Express project with Typescript and live auto-reloading. Note that this metho...