TypeScript · Junior · Question 30 of 75

How does enum Color { Red, Green } differ from type Color = 'red' | 'green'?

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.

Further reading

Problem with a question

TypeScript · Junior · question 30

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz