Recall & Review
beginner
What is a TypeScript enum?
An enum is a special TypeScript type that allows you to define a set of named constants, which can be either numeric or string values.
Click to reveal answer
beginner
What is a union literal type in TypeScript?
A union literal type is a type that can be one of several specific string or numeric literal values, written as a union of those literals.
Click to reveal answer
intermediate
Name one advantage of using enums over union literal types.
Enums provide a clear grouping of related constants and can be used both as types and values, which helps with code organization and readability.
Click to reveal answer
intermediate
What is a key trade-off when choosing union literal types instead of enums?
Union literal types are simpler and generate less JavaScript code, but they lack runtime representation and cannot be iterated over like enums.
Click to reveal answer
beginner
Why might you prefer union literal types for small sets of values?
Because union literal types are lightweight, have no runtime overhead, and provide better type safety with string literal types, making them ideal for small, fixed sets.
Click to reveal answer
Which of the following is true about TypeScript enums?
✗ Incorrect
Enums generate JavaScript code and exist at runtime, unlike union literal types which are erased after compilation.
What is a benefit of union literal types compared to enums?
✗ Incorrect
Union literal types are erased during compilation, so they generate no extra JavaScript code.
Which is a limitation of union literal types?
✗ Incorrect
Union literal types do not exist at runtime, so you cannot iterate over their values.
When might enums be preferred over union literal types?
✗ Incorrect
Enums provide runtime objects that can be iterated, unlike union literal types.
Which statement about union literal types is false?
✗ Incorrect
Union literal types do not generate runtime JavaScript objects; they exist only at compile time.
Explain the main differences between enums and union literal types in TypeScript.
Think about runtime presence and code generation.
You got /5 concepts.
Describe scenarios where you would choose union literal types over enums and vice versa.
Consider runtime needs and code simplicity.
You got /4 concepts.