Bird
Raised Fist0
Typescriptprogramming~10 mins

Const enums and optimization in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Const enums and optimization
Define const enum
↓
Use enum values in code
↓
Compile TypeScript
↓
Compiler replaces enum references with values
↓
Output JavaScript without enum object
↓
Run optimized JavaScript code
This flow shows how const enums are defined and then replaced by their values during compilation, resulting in optimized JavaScript without extra enum objects.
Execution Sample
Typescript
const enum Colors {
  Red = 1,
  Green,
  Blue
}

let c = Colors.Green;
Defines a const enum Colors and assigns Colors.Green to variable c, which compiles to direct value usage.
Execution Table
StepCode LineActionCompilation OutputResult
1const enum Colors { Red = 1, Green, Blue }Define const enumNo JS output for enum objectEnum values known at compile time
2let c = Colors.Green;Use enum valuelet c = 2;Enum reference replaced by value 2
3Run JS codeVariable c assignedc = 2Variable c holds number 2
4EndNo further code-Execution complete
💡 Compilation replaces enum references with values, so no enum object exists at runtime.
Variable Tracker
VariableStartAfter assignmentFinal
cundefined22
Key Moments - 3 Insights
Why don't we see an enum object in the compiled JavaScript?
Because const enums are replaced by their values at compile time, no enum object is generated, as shown in execution_table row 1 and 2.
What value does variable 'c' hold after assignment?
Variable 'c' holds the numeric value 2, which is the value of Colors.Green, as shown in execution_table row 3 and variable_tracker.
Can we use const enums with computed or dynamic values?
No, const enums must have constant values so the compiler can replace them; this is why the enum values are simple numbers here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the compiled JavaScript for 'let c = Colors.Green;'?
Alet c = Colors.Green;
Blet c = 2;
Clet c = Colors["Green"];
Dlet c = 1;
💡 Hint
Check execution_table row 2 where enum reference is replaced by value.
According to variable_tracker, what is the value of 'c' after assignment?
A2
B1
Cundefined
D3
💡 Hint
Look at variable_tracker row for 'c' after assignment.
If we change 'const enum' to 'enum', what happens to the compiled output?
ANo change in output
BEnum references replaced by values
CEnum object is generated in JS
DCompilation error
💡 Hint
Const enums are replaced by values, normal enums generate objects (see concept_flow).
Concept Snapshot
const enum syntax:
const enum Name { A = 1, B, C }

Behavior:
- Enum values replaced at compile time
- No enum object in output JS
- Optimizes runtime performance

Key rule:
Use only constant values for const enums
Full Transcript
Const enums in TypeScript are special enums that the compiler replaces with their values during compilation. This means no enum object is created in the JavaScript output, making the code faster and smaller. For example, defining a const enum Colors with values Red=1, Green=2, Blue=3 and using Colors.Green in code will compile to just the number 2. The variable assigned Colors.Green will hold the number 2 at runtime. Const enums must have constant values so the compiler can replace them. If you use a normal enum instead, the compiler generates an enum object in JavaScript. This optimization helps keep the code efficient.

Practice

(1/5)
1. What is the main benefit of using const enum in TypeScript?
easy
A. It allows enums to be modified at runtime.
B. It enables enums to be serialized to JSON automatically.
C. It replaces enum references with actual values at compile time, reducing runtime code.
D. It creates a separate JavaScript object for the enum at runtime.

Solution

  1. Step 1: Understand what const enum does

    const enum replaces enum references with their actual values during compilation, so no enum object exists at runtime.
  2. Step 2: Compare runtime behavior

    Unlike regular enums, const enum does not generate extra JavaScript code for the enum object, reducing code size and improving speed.
  3. Final Answer:

    It replaces enum references with actual values at compile time, reducing runtime code. -> Option C
  4. Quick Check:

    Const enums optimize code by replacing references with values [OK]
Hint: Const enums inline values, no runtime object created [OK]
Common Mistakes:
  • Thinking const enums create runtime objects
  • Believing const enums allow runtime modification
  • Confusing const enums with regular enums
2. Which of the following is the correct syntax to declare a const enum in TypeScript?
easy
A. enum Colors { Red, Green, Blue }
B. const enum Colors { Red, Green, Blue }
C. constant enum Colors { Red, Green, Blue }
D. enum const Colors { Red, Green, Blue }

Solution

  1. Step 1: Recall the correct keyword order

    The correct syntax uses the keyword const before enum to declare a const enum.
  2. 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.
  3. Final Answer:

    const enum Colors { Red, Green, Blue } -> Option B
  4. Quick Check:

    Const enum syntax = const enum [OK]
Hint: Remember: const comes before enum in declaration [OK]
Common Mistakes:
  • Writing 'constant' instead of 'const'
  • Placing 'const' after 'enum'
  • Omitting 'const' keyword
3. Given the following code, what will be the output when compiled and run?
const enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}

const move = Direction.Left;
console.log(move);
medium
A. 3
B. undefined
C. Left
D. 2

Solution

  1. Step 1: Understand enum value assignment

    The enum starts with Up = 1, so Down = 2, Left = 3, and Right = 4.
  2. Step 2: Identify the value of Direction.Left

    It is 3, so move is assigned 3, and console.log(move) prints 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Enum Left value = 3 [OK]
Hint: Count enum values starting from assigned number [OK]
Common Mistakes:
  • Assuming Left is 2 instead of 3
  • Expecting string output 'Left'
  • Confusing enum with object property
4. What is the error in the following TypeScript code using const enums?
const enum Status {
  Active = 1,
  Inactive = 0
}

console.log(Status[1]);
medium
A. Compile-time error: Cannot use reverse mapping with const enums
B. Runtime error: Status is undefined
C. No error, outputs 'Active'
D. Outputs 1

Solution

  1. Step 1: Understand const enum limitations

    Const enums do not generate an object at runtime, so reverse mapping (accessing by index) is not supported.
  2. Step 2: Identify the error cause

    Using Status[1] tries to access reverse mapping, causing a compile-time error.
  3. Final Answer:

    Compile-time error: Cannot use reverse mapping with const enums -> Option A
  4. Quick Check:

    Reverse mapping disallowed in const enums [OK]
Hint: Const enums have no reverse mapping, avoid index access [OK]
Common Mistakes:
  • Expecting reverse mapping to work with const enums
  • Thinking it outputs string 'Active'
  • Ignoring compile-time errors
5. You want to optimize a TypeScript program by replacing a regular enum with a const enum. Which of the following changes will NOT work correctly after this replacement?
hard
A. Using enum values directly in expressions and comparisons.
B. Using enum values as keys in a switch statement.
C. Assigning enum values to variables for later use.
D. Accessing enum values by their numeric index (reverse mapping).

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Accessing enum values by their numeric index (reverse mapping). -> Option D
  4. Quick Check:

    Reverse mapping breaks with const enums [OK]
Hint: Const enums disallow reverse mapping, avoid numeric index access [OK]
Common Mistakes:
  • Assuming all enum features work with const enums
  • Using reverse mapping without error
  • Confusing const enum with regular enum behavior
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard