TypeScript · Lesson 40 of 120
Enums
Named constants.
An enum is a set of named constants. TypeScript adds them as a real object in your JavaScript. They're common in older code, so it's worth knowing them.

- Named constants
- A real object
- Common in older code
A size enum
Size is an enum with three members, each given a string value. We use size dot medium like any constant. Run it.
app.tstypescript
enum Size {
Small = "S",
Medium = "M",
Large = "L",
}
function frameFor(height: number): Size {
return height < 165 ? Size.Small : height < 180 ? Size.Medium : Size.Large;
}
console.log(frameFor(172), Size.Large);Output
M L
Enum or literal union
A union of string literals does the same job, with no extra code at runtime. Many teams prefer literal unions today. Enums are still fine, especially when you need a real object to loop over.
enum
- Size.MediumReal object at runtimeOlder style
Literal union
- 'M'Types onlyModern favourite
Summary
Enums are named constants. Literal unions often do the same job more simply. Next, as const.
Key takeaways- enum Size
- Size.Medium
- Or a literal union
Next up: as const — the tightest types