TypeScript Enum with String Values

In this example shows how to create and use TypeScript string enums with examples.
In TypeScript, enums are set of named constants. Though it is optional, enums should be a named group of related constants. 

Typescript String-based Enums

In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member.
Check out enums in Java at Java Enums Tutorial.
Enum syntax is very similar to other languages e.g. Java.
// Example 2 - String-based Enums
enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}

Typescript String-based Enums Examples

Example 1: How to create String-based enum in Typescript

Let's create a typescript file named enums.ts and add following code to it:
// Example 2 - String-based Enums
enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}

// Access enum member values
console.log(Direction.Up);
console.log(Direction.Down);
console.log(Direction.Left);
console.log(Direction.Right);

console.log(Direction['Up']);
console.log(Direction['Down']);

Example 2: How to access enum member values

Use Enum.member or Enum['member'] format to access enum member values. For Example:
console.log(Direction.Up);
console.log(Direction.Down);
console.log(Direction['Up']);
console.log(Direction['Down']);
Let's run above typescript file using the following command:
c:\user\typescript-tutorial> tsc .\enums.ts
Look at the generated code in JavaScript. It has a generated lookup table like this.
var Direction;
(function (Direction) {
    Direction["Up"] = "UP";
    Direction["Down"] = "DOWN";
    Direction["Left"] = "LEFT";
    Direction["Right"] = "RIGHT";
})(Direction || (Direction = {}));
// Access enum member values
console.log(Direction.Up);
console.log(Direction.Down);
console.log(Direction.Left);
console.log(Direction.Right);
console.log(Direction['Up']);
console.log(Direction['Down']);
The above file created as enums.js file. Now we don't need a browser, we can use node.js to run this javascript file. Let's run this enums.js file using the following command:
c:\user\typescript-tutorial> node .\enums.js
UP
DOWN
LEFT
RIGHT
UP
DOWN
Follow similar steps to run below typescript examples.

Example 3: LogLevel Enum

// Example 1- String-based Enums
export enum LogLevel {
    Off = "Off",
    Fatal = "Fatal",
    Error = "Error",
    Warn = "Warn",
    Info = "Info",
    Debug = "Debug"
}

// Access enum member values
console.log(LogLevel.Debug);
console.log(LogLevel.Error);
console.log(LogLevel.Warn);
console.log(LogLevel.Info);
console.log(LogLevel.Fatal);

console.log(LogLevel['Fatal']);
console.log(LogLevel['Debug']);
console.log(LogLevel['Info']);

Example 4: Weekday Enum

// Example 3 - String-based Enums
enum Weekday {
    Monday = "Monday",
    Tuesday = "Tuesday",
    Wednesday = "Wednesday",
    Thursday = "Thursday",
    Friday = "Friday",
    Saturday = "Saturday",
    Sunday = "Sunday"
}

// Access enum member values
console.log(Weekday.Monday);
console.log(Weekday.Tuesday);
console.log(Weekday.Wednesday);
console.log(Weekday.Thursday);
console.log(Weekday.Friday);
console.log(Weekday.Saturday);
console.log(Weekday.Sunday);

console.log(Weekday['Saturday']);
console.log(Weekday['Sunday']);

Example 5: Enum as a function argument

To pass enum in functions, declare the argument type of enum itself.
enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}

function checkDirection(direction: Direction){
    console.log(direction);
}

checkDirection(Direction.Up);
checkDirection(Direction.Down);
checkDirection(Direction.Left);
checkDirection(Direction.Right);
Output:
C:\user\typescript-tutorial> tsc .\enums.js
C:\user\typescript-tutorial> node .\enums.js
UP
DOWN
LEFT
RIGHT
Learn more about TypeScript at TypeScript Tutorial with Examples.

Comments