Skip to main content

Enum

Enum type

You can have merits of auto-completion in typescript enum. But there's a side effect

enum ArrowKey {
UP= "up",
DOWN = "down",
LEFT = "left",
RIGHT = "right"
}
info

When .ts file is transpiling to .js, there's a IIFE(Immediately Invoked Function Expression).
And IIFE would be excluded from tree-shaking which supports removing dead code automatically when building multiple js files into single file.

const ARROW_KEY = {
UP: "up",
DOWN : "down",
LEFT : "left",
RIGHT : "right"
} as const;

type ARROW_KEY_TYPE = keyof typeof ARROW_KEY;

Using webpack bundler, suppose you only use the loadash.fill() method only
then, tree shaking should be executed which removing other package methods and leaving just only .fill() method in bundled file.

📝 Conclusion

tip

Use literal type and keyof , typeof keyword instead of enum.