DEV Community

Cover image for You might not need TypeScript Enum

You might not need TypeScript Enum

Muhammad A Faishal on September 05, 2023

In TypeScript, there is a feature called Enum which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named c...
Collapse
 
wraith profile image
Jake Lundberg

Great topic choice!

I used to love enums, but once support for as const was released, I stopped using them entirely for the exact reasons you laid out here. I never really liked that enums were the one thing in TS that didn't get stripped out during build...instead, it just gets mutated and added to the bundle.

Nice explanations! Keep up the great work!

Collapse
 
maafaishal profile image
Muhammad A Faishal

I'm glad to know that!

Collapse
 
lexakaren profile image
lexa

I found your post about TypeScript Enums really interesting and thought-provoking. It's great to see different perspectives on how to approach common programming tasks.

While I'm a fan of TypeScript Enums for the type safety they provide, your article makes some valid points about alternative ways to handle certain situations. It's crucial for developers to be aware of different approaches and choose the one that best fits the specific needs of their project.

What I appreciate most about your post is that it encourages a healthy discussion about TypeScript's strengths and potential alternatives, which can help us all become better developers. Thanks for sharing your insights and sparking this conversation!

I believe the best approach often depends on the context of the project and the development team's preferences. Keep the great articles coming, and I'm looking forward to more thought-provoking discussions in the future!

Collapse
 
maafaishal profile image
Muhammad A Faishal

Yeah, it does. Sometimes speed is prioritized over performance in a project...

Anyway, thanks for the kind feedback!

Collapse
 
atinypixel profile image
Aziz Kaukawala

Seems introspective! Gonna try this approach!
Thanks Muhammad!

Collapse
 
maafaishal profile image
Muhammad A Faishal

Cool!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
maafaishal profile image
Muhammad A Faishal

We use as const since it can infer the property values following typescriptlang.org/docs/handbook/e....

Speaking of Object.freeze, I've tried it and the result is it couldn't infer the property values. Maybe, can you give the example?

Collapse
 
mmvergara profile image
mmvergara • Edited

oh it doesn't infer?, im sorry didn't knew I was just guessing.

Thread Thread
 
maafaishal profile image
Muhammad A Faishal

No, it doesn't...

Collapse
 
sergeyshandar profile image
Sergey Shandar

I'm avoiding TypeScript enums with all cost. I prefer to use unions instead

type Color = 'RED' | 'GREEN' | 'BLUE';
Enter fullscreen mode Exit fullscreen mode

This way, TypeScript doesn't need to generate extra hidden code. Unions are also compatible with JavaScript type annotations proposal but enums are not.

Collapse
 
maafaishal profile image
Muhammad A Faishal • Edited

Yeah, ultimately, the best approach is to go back to the basics.

Anyway, wow JS will be able to accept type annotations by itself. It's great news!

Collapse
 
ismaelguerrib profile image
Ismaël Guerrib

Thanks Muhammad for this nice explanations !

Using TypeORM in a CRUD app builder, do you think that this preference for const over enum should also apply to databases or the concept of Enum is getting to the developer precious documentation in his development experience which can not really be mesurable ?

It really gives me food for thought !

Collapse
 
maafaishal profile image
Muhammad A Faishal

Personally, I've never tried TypeORM and CASE. But, if it needs to transpile the source code, I suggest you to replace Enum. Otherwise, using Enum is not a problem.

Anyway, you can do you own research on the execution time to check whether Enum can increase the execution time or not.

Collapse
 
calinzbaenen profile image
Calin Baenen

What does the as const do? What if I were to remove it?

Collapse
 
maafaishal profile image
Muhammad A Faishal

Here is the difference

Image description

Collapse
 
smoni profile image
SMoni

Nice article... I've created a small snippet to create an enum only by the provided strings

stackblitz.com/edit/js-kngxa2?file...

Collapse
 
maafaishal profile image
Muhammad A Faishal

Yeah, it can be an option for someone who is lazy enough to define the object with as const. My feedback is don't forget to support TypeScript.

By the way, it's good!.

Collapse
 
dangduytung profile image
dangduytung

With TypeScript, It looks like simple expression and to understand easily

Collapse
 
maafaishal profile image
Muhammad A Faishal

Yeah, it does

Collapse
 
xyangst profile image
Angst

did you forget number enums? not having to provide a index is a huge bonus imo, also calling a function with a string isnt really easy to refactor later on