An enum in TypeScript is a way to define a set of named constants. It creates an object at runtime that maps names to values and sometimes values back to names.
Click to reveal answer
intermediate
Why can enums add unnecessary runtime code?
Because enums generate JavaScript objects at runtime, they add extra code even if you only need compile-time type checking or simple constants.
Click to reveal answer
intermediate
What is a common alternative to enums to avoid runtime code?
Using union types of string or number literals combined with 'as const' objects can provide type safety without generating runtime code.
Click to reveal answer
advanced
How does a const enum differ from a regular enum in TypeScript?
A const enum is inlined at compile time and does not generate runtime code, but it requires 'preserveConstEnums' to be false and can cause issues with some tools.
Click to reveal answer
advanced
What is a downside of using const enums?
Const enums are erased during compilation, so if you use them with external JavaScript or certain build tools, it can cause errors because the enum object does not exist at runtime.
Click to reveal answer
What does a regular TypeScript enum generate in JavaScript?
AA function that returns enum values
BOnly type information, no JavaScript code
CAn object with keys and values representing the enum members
DA string literal union type
✗ Incorrect
Regular enums create an object at runtime mapping names to values and sometimes values back to names.
Which TypeScript feature avoids generating runtime code for enums?
Aconst enum
Binterface
Cclass
Dnamespace
✗ Incorrect
Const enums are inlined at compile time and do not produce runtime JavaScript code.
What is a simple alternative to enums to get type safety without runtime code?
AUsing functions
BUnion of string literals with 'as const' object
CUsing interfaces
DUsing classes with static properties
✗ Incorrect
Union types with 'as const' objects provide type safety and no runtime enum object is created.
What problem can const enums cause in some build setups?
ARuntime errors because enum objects do not exist
BSlower runtime performance
CLarger bundle size
DLack of type safety
✗ Incorrect
Because const enums are erased, if external code expects the enum object, it can cause runtime errors.
When should you avoid using regular enums in TypeScript?
AWhen you want to use interfaces
BWhen you need runtime reflection
CWhen you want to use classes
DWhen you want to avoid extra runtime code
✗ Incorrect
Regular enums add runtime code, so avoid them if you want minimal JavaScript output.
Explain why regular enums in TypeScript can add unnecessary runtime code and how to avoid it.
Think about what code is generated and alternatives that only exist at compile time.
You got /3 concepts.
Describe the trade-offs between using regular enums and const enums in TypeScript.
Consider runtime presence and compatibility.
You got /4 concepts.
Practice
(1/5)
1. What is a main reason to avoid using regular enums in TypeScript when you only need type checking?
easy
A. They add extra runtime code that may be unnecessary.
B. They cause syntax errors in modern TypeScript versions.
C. They cannot be used with functions.
D. They do not support string values.
Solution
Step 1: Understand what regular enums generate
Regular enums create extra JavaScript code that runs when your program runs, even if you only want type checks.
Step 2: Identify the impact of extra runtime code
This extra code can increase bundle size and slow down execution unnecessarily if you only need compile-time type safety.
Final Answer:
They add extra runtime code that may be unnecessary. -> Option A
Quick Check:
Extra runtime code = Avoid regular enums when only type checks needed [OK]