DEV Community


Discussion on: Managing Key-Value Constants in TypeScript

michaeljota profile image
Michael De Abreu • Edited

What is advantage between this and a string enum?

export enum Colors {
  blue = 'Blue',
  green = 'Green',
  red = 'Red',

String enums are transformed to regular objects, so you could access them as, just like in your example.

lacolaco profile image
Suguru Inatomi Author

As far as I know, there isn't an advantage technically. But I prefer union types because enum is not an ECMAScript's standard syntax.
After transpilation, output code with a tuple and an object is similar to the original TS code, but output code with enum is different from the original TS code a lot.
I think a big value of TypeScript is the mindset; it is a superset of ECMAScript. So I don't want to use enum.

michaeljota profile image
Michael De Abreu

As long as you know what it becomes, I always suggest the usage of the languages features. Whatever they are.

woodywoodsta profile image
Sean Wood

I hear what you're saying! But that's similar to buying a bigger car and never putting anything in the extra space because it wasn't available in your standard car.

I think that the choice of adopting Typescript as a language comes with the acceptance of a build step, the outcome of which is up to the compiler.