How to Convert Enum to String in TypeScript

To convert Enum to String in TypeScript, use the “bracket notation ([ ])” to access a specific value on the enum to get its name.

enum Colors {
  Red,
  Green,
  Blue
}

let color: Colors = Colors.Red;

console.log(Colors[color]);

Output

Red

Converting a Numeric enum to a string

If you have a numeric enum and you want to convert one of its values to a string representation, you can use the enum itself as a kind of lookup.

enum Days {
  Sunday,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
}

console.log(Days[3]);

console.log(Days[Days.Wednesday]);

Output

Converting a Numeric enum to a string

Converting a Numeric enum to a number

To convert a numeric enum to a number, “access a specific property using dot notation.”

enum Days {
  Sunday,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
}

const num = Days.Wednesday;

console.log(num);

console.log(typeof num);

Output

3
number

Converting a string enum to a string

To convert a string enum to a string, access one of the names in the enum using “dot notation.”

enum Days {
  Sunday = 'S',
  Monday = 'M',
  Tuesday = 'T',
  Wednesday = 'W',
  Thursday = 'T',
  Friday = 'F',
  Saturday = 'S'
}

const str = Days.Wednesday;

console.log(str);

console.log(typeof str);

Output

Converting a string enum to a string

The Object.keys() method returns an array containing the enum’s names and values, so you have to use the filter method to filter out the unnecessary values.

Related posts

TypeScript String to Enum

TypeScript String to Number

TypeScript Enum to Array

TypeScript String to Boolean