DEV Community

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

Collapse
abhinav1217 profile image
Abhinav Kulshreshtha

Your case is valid if the variable being used is a constant, in that case a const enum would be best of both world.

But imagine you are writing a game, and game states needs to be manipulate at runtime. In that case, it would be required to bundle entire object in final build. An Enum of game state would be better, especially if you are making a game library, which would be used by other developers ( who may be an old fashion JS devs).

Collapse
abhinav1217 profile image
Abhinav Kulshreshtha • Edited on

Real use case scenario of union types would be something like this

type StringOrNumber = string | number;
let code: StringOrNumber;
code = 123;   // OK
code = "ABC"; // OK
code = false; // Compiler Error
Enter fullscreen mode Exit fullscreen mode