๐ข Enums vs Union Types
Information
Enums allow developers to define a set of named constants that represent a specific type. TypeScript provides both numeric and string-based enums.
On the other hand, Union Types are simpler than enums, as they donโt require any additional runtime or compilation overhead.
Reasons to use Union Types
Enums are a concept borrowed from languages like C# and Java. TypeScript enums are compiled into JavaScript objects with keys for both the names and values of the enum members. This results in larger output files and additional memory consumption, which can be particularly problematic in performance-critical applications.
Enums have some more pitfalls as well:
- Non-const enums do not fit the concept of โa typed superset of JavaScript.โ They violate the concept by emitting global JavaScript objects that live in runtime with a syntax that is not compatible with JavaScript (JavaScript uses dynamic typing rather than static typing; enums are a form of static typing). Since JavaScript has no compilation step, there is little or no value in having static typing.
- Const enums, in contrast, cannot be transpiled with Babel. But there are workarounds for this issue, e.g., using the
babel-plugin-const-enum
plugin. The TypeScript documentation about const enums says โDo not use const enums at allโ. - To use enums, you have to import them. If you want to use enum values in a template, youโll need to declare a variable in your component too.
- Numeric enums are not type safe โฆ
Reasons to use Enums
Enums are the best option for code maintainability. It is easy to find all usages of a value in a project and enforce constraints. If you stick to assigning strings to the enum keys all the time, you can avoid a lot of issues.
Itโs true that enums produce larger output files, but thatโs not always a real problem. As long as the enum does its job without any problems, it shouldnโt be something you care that much about.
Another good thing is that the necessary enum prefixes add meaning to otherwise meaningless values, so they can improve readability. For example, HttpStatus.Forbidden
gives more information than Forbidden
.
Mapped types
A mapped type is the process of creating a new type by mapping type information from an existing type.
Conclusion
Enums are not redundant, but in most cases, union types are preferred. Unless you care a lot about maintainability, enums may fit better. Here are some more interesting articles discussing this subject:
- Should You Use Enums or Union Types in Typescript?
- Typescript has unions, so are enums redundant?
- Tidy TypeScript: Prefer union types over enums
Statement
The goal of this challenge is to refactor the enums Difficulty
and Direction
.
- Refactor the
Difficulty
enum to union type. - Refactor the
Direction
enum to mapped type.