0
0
Typescriptprogramming~5 mins

Const enums and optimization in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
Which TypeScript compiler flag can cause errors when using const enum?
A--isolatedModules
B--noImplicitAny
C--strictNullChecks
D--esModuleInterop
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
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
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 }
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.