Recall & Review
beginner
What is a TypeScript enum?
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?
✗ 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?
✗ 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?
✗ 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?
✗ 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?
✗ 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.