A <code>const enum</code> is a special kind of enum that is completely inlined at compile time, meaning no JavaScript code is generated for the enum itself. Instead, enum values are replaced directly with their numeric or string values.
Click to reveal answer
intermediate
How does using <code>const enum</code> improve performance?
Using <code>const enum</code> reduces the generated JavaScript code size and runtime overhead because enum references are replaced with literal values, avoiding extra object lookups.
Click to reveal answer
advanced
What happens if you try to use a <code>const enum</code> with the <code>--isolatedModules</code> flag in TypeScript?
The compiler will give an error because <code>const enum</code> values are inlined and not emitted as code, which conflicts with isolated modules that require each file to be independently compilable.
Click to reveal answer
beginner
Example: What is the output JavaScript for this TypeScript code?<br><pre>const enum Colors { Red, Green, Blue }
let c = Colors.Green;</pre>
The compiled JavaScript will be:<br><pre>let c = 1;</pre><br>Because <code>Colors.Green</code> is replaced with its numeric value <code>1</code> directly.
Click to reveal answer
intermediate
Why should you avoid using <code>const enum</code> in libraries published as npm packages?
Because <code>const enum</code> values are inlined at compile time, consumers of the library must also compile the TypeScript source. If they use plain JavaScript or different compilation settings, the enum values may not be available, causing errors.
Click to reveal answer
What does a const enum generate in the compiled JavaScript?
AAn error during compilation
BA full enum object with keys and values
CNo enum object; values are inlined as literals
DA function to access enum values
✗ Incorrect
Const enums are fully inlined, so no enum object is generated in JavaScript.
Which TypeScript compiler flag can cause errors when using const enum?
A--isolatedModules
B--noImplicitAny
C--strictNullChecks
D--esModuleInterop
✗ Incorrect
The --isolatedModules flag requires each file to be independently compilable, which conflicts with const enums.
Why might const enum improve runtime speed?
ABecause it avoids object property lookups
BBecause it caches enum values in memory
CBecause it uses async functions
DBecause it compiles to WebAssembly
✗ Incorrect
Inlining enum values avoids the cost of looking up properties on an enum object at runtime.
What is a risk of using const enum in distributed JavaScript code?
AIt causes runtime memory leaks
BEnum values may be missing if code is not compiled with TypeScript
CIt slows down the code execution
DIt increases bundle size
✗ Incorrect
Since const enums are inlined at compile time, if the consumer uses plain JavaScript, enum values won't exist.
Which of these is a correct way to declare a const enum?
Aconst Direction enum { Up, Down }
Benum const Direction { Up, Down }
Cenum Direction const { Up, Down }
Dconst enum Direction { Up, Down }
✗ Incorrect
The correct syntax is const enum Name { ... }.
Explain what a const enum is and how it helps optimize TypeScript code.
Think about how the enum values appear in the final JavaScript.
You got /4 concepts.
Describe a situation where using const enums might cause problems in a project.
Consider how code is shared and compiled across different environments.
You got /4 concepts.
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
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.
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.
Final Answer:
It replaces enum references with actual values at compile time, reducing runtime code. -> Option C
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
Step 1: Recall the correct keyword order
The correct syntax uses the keyword const before enum to 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 B
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
Step 1: Understand enum value assignment
The enum starts with Up = 1, so Down = 2, Left = 3, and Right = 4.
Step 2: Identify the value of Direction.Left
It is 3, so move is assigned 3, and console.log(move) prints 3.
Final Answer:
3 -> Option A
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
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
Using Status[1] tries to access reverse mapping, causing a compile-time error.
Final Answer:
Compile-time error: Cannot use reverse mapping with const enums -> Option A
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
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 D