Open In App

Explain the concept of enums in TypeScript ?

Enum in TypeScript is a shorthand used for the enumerations. Enum is a way of defining multiple related named constants at the same place in the form of a collection which can be later accessed and assigned to other variables in the code. By default, the enums are assigned with number type values starting from 0. You can also assign them with the string values using the assignment operator(=). It helps to enhance the code readability and maintainability by defining meaningful identifiers for the magic strings and the numbers.

Syntax:

enum enumName {
// Assigned with default number value 0
property1,
// Assigned with the given string value
property2 = "stringValue",
}
Article Tags :