DEV Community

Discussion on: We don't have to use Enums on TypeScript? 【Discussion】

716green profile image
Bob Bass

I just answered a question on quora last week where someone was asking why we need enums. It was a good question so I did a little bit of research first.

It turns out that the data type was originally created when we had far less computing power and integers were used as identifiers for things that we would use hash tables or switch statements for today. The purpose was to make the code more readable during some of the most primitive periods of computer programming.

I'm a software engineer and these days I'm working mostly within the JavaScript ecosystem. I write a lot of TypeScript and I'm a firm believer that you should only use language features when it makes sense to do so, you shouldn't use them just because they exist. You should always choose the option that is the most readable, clean, and organized for your particular use case.

I can't even tell you the last time I used an Enum in production. There is really just no good reason. I was racking my brain as I tried to come up with a good use case for the person who asked the question. The only things that I could come up with were so convoluted that it made me realize that I don't need Enums, not with the modern web application work I'm doing these days.

It's possible that you might use an Enum if you are converting a value from MySQL from tinyint into a Boolean, but I'd rather use a ternary operator instead.

If someone can think of a good use case, I'd really love to hear it.

errorgamer2000 profile image
ErrorGamer2000 • Edited on

Similar to Volodymyr Yepishev, I personally think that they are extremely useful for error codes, because you could seperate them into named groups that are easily readable. This would probably be the most useful in larger libraries.