TypeScript · Junior · Question 30 of 75
How does enum Color { Red, Green } differ from type Color = 'red' | 'green'?
- ✓Correct answer: An
enumcreates an object at runtime; a literal union exists only in types - 2A literal union creates an object at runtime; an
enumis only a type - 3No difference: the compiler generates the same JavaScript for both
Explanation
A regular enum compiles to a JS object (a numeric one with reverse mapping). A string union adds nothing to the bundle and is compatible with JSON and APIs. Many teams prefer unions or an as const object. Also, enums are incompatible with erasableSyntaxOnly mode.