Enum
Enum type
You can have merits of auto-completion in typescript enum. But there's a side effect
- enum.ts
- enum.js
enum ArrowKey {
UP= "up",
DOWN = "down",
LEFT = "left",
RIGHT = "right"
}
"use strict"
var ArrowKey;
(function (ArrowKey) {
ArrowKey["UP"] = "up";
ArrowKey["DOWN"] = "down";
ArrowKey["LEFT"] = "left";
ArrowKey["RIGHT"] = "right";
// IIFE Code
})(ArrowKey || (ArrowKey = {}));
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.
- literalType.ts
- literalType.js
const ARROW_KEY = {
UP: "up",
DOWN : "down",
LEFT : "left",
RIGHT : "right"
} as const;
type ARROW_KEY_TYPE = keyof typeof ARROW_KEY;
const ARROW_KEY = {
UP: "up",
DOWN: "down",
LEFT: "left",
RIGHT: "right"
};
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.