Add description attribute to enum and read this description in TypeScript

Another interesting solution found here is using ES6 Map:

export enum Sample {
  V,
  IV,
  III
}

export const SampleLabel = new Map<number, string>([
  [Sample.V, 'FIVE'],
  [Sample.IV, 'FOUR'],
  [Sample.III, 'THREE']
]);

USE

console.log(SampleLabel.get(Sample.IV)); // FOUR

SampleLabel.forEach((label, value) => {
  console.log(label, value);
});

// FIVE 0
// FOUR 1
// THREE 2  

Leave a Comment