Recall & Review
beginner
What is a <code>const enum</code> in TypeScript?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?✗ 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?✗ Incorrect
The
--isolatedModules flag requires each file to be independently compilable, which conflicts with const enums.Why might
const enum improve runtime speed?✗ 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?✗ 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?
✗ 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.