What if you could make your app faster just by changing how you write enums?
Why Const enums and optimization in Typescript? - Purpose & Use Cases
Imagine you have a list of fixed values like colors or directions used all over your code. You write them as regular enums and use them everywhere. But when you check the final JavaScript, you see extra code for these enums, making your app bigger and slower.
Using regular enums means the compiler generates extra JavaScript objects and lookups. This adds unnecessary code size and runtime overhead. It's like carrying a heavy toolbox when you only need a single screwdriver. This slows down your app and wastes memory.
Const enums tell TypeScript to replace enum references with their actual values during compilation. This removes the extra enum objects and lookups, making your code smaller and faster. It's like having the screwdriver already in your hand, ready to use instantly.
enum Direction { Up, Down, Left, Right }
let move = Direction.Up;const enum Direction { Up, Down, Left, Right }
let move = Direction.Up;Const enums enable your code to run faster and be smaller by removing unnecessary enum objects, making your apps more efficient and lightweight.
In a game app, directions like Up, Down, Left, Right are used everywhere. Using const enums means the game runs smoother and loads faster on players' devices.
Regular enums add extra code and slow down your app.
Const enums replace enum references with values at compile time.
This leads to smaller, faster, and more optimized JavaScript output.
Practice
const enum in TypeScript?Solution
Step 1: Understand what
const enumdoesconst enumreplaces enum references with their actual values during compilation, so no enum object exists at runtime.Step 2: Compare runtime behavior
Unlike regular enums,const enumdoes not generate extra JavaScript code for the enum object, reducing code size and improving speed.Final Answer:
It replaces enum references with actual values at compile time, reducing runtime code. -> Option CQuick Check:
Const enums optimize code by replacing references with values [OK]
- Thinking const enums create runtime objects
- Believing const enums allow runtime modification
- Confusing const enums with regular enums
Solution
Step 1: Recall the correct keyword order
The correct syntax uses the keywordconstbeforeenumto declare a const enum.Step 2: Check each option
const enum Colors { Red, Green, Blue } matches the correct syntax:const enum Colors { Red, Green, Blue }. Options C and D use invalid keywords or order.Final Answer:
const enum Colors { Red, Green, Blue } -> Option BQuick Check:
Const enum syntax = const enum [OK]
- Writing 'constant' instead of 'const'
- Placing 'const' after 'enum'
- Omitting 'const' keyword
const enum Direction {
Up = 1,
Down,
Left,
Right
}
const move = Direction.Left;
console.log(move);Solution
Step 1: Understand enum value assignment
The enum starts withUp = 1, soDown = 2,Left = 3, andRight = 4.Step 2: Identify the value of
It is 3, soDirection.Leftmoveis assigned 3, andconsole.log(move)prints 3.Final Answer:
3 -> Option AQuick Check:
Enum Left value = 3 [OK]
- Assuming Left is 2 instead of 3
- Expecting string output 'Left'
- Confusing enum with object property
const enum Status {
Active = 1,
Inactive = 0
}
console.log(Status[1]);Solution
Step 1: Understand const enum limitations
Const enums do not generate an object at runtime, so reverse mapping (accessing by index) is not supported.Step 2: Identify the error cause
UsingStatus[1]tries to access reverse mapping, causing a compile-time error.Final Answer:
Compile-time error: Cannot use reverse mapping with const enums -> Option AQuick Check:
Reverse mapping disallowed in const enums [OK]
- Expecting reverse mapping to work with const enums
- Thinking it outputs string 'Active'
- Ignoring compile-time errors
Solution
Step 1: Recall const enum behavior
Const enums inline values and do not create runtime objects, so reverse mapping (index to name) is not available.Step 2: Analyze each option
Options A, B, and D work fine as they use the inlined values directly. Accessing enum values by their numeric index (reverse mapping). requires reverse mapping, which fails with const enums.Final Answer:
Accessing enum values by their numeric index (reverse mapping). -> Option DQuick Check:
Reverse mapping breaks with const enums [OK]
- Assuming all enum features work with const enums
- Using reverse mapping without error
- Confusing const enum with regular enum behavior
Ready to prove it?
Pass the test and this pattern moves unknown
