Ad space (banner)
Lesson 12 / 20

enum (Enumerated Types)

This lesson covers enum, used to name and manage a fixed set of choices. It's for anyone searching "TypeScript enum usage."

enum is a way to group related constants under one name. It's used for a similar purpose to the literal types from the previous lesson, but because the value itself gets a name, the code's meaning is easier to convey.

The example defines enum Weekday { Mon, Tue, Wed, Thu, Fri, Sat, Sun }. Unless specified otherwise, Mon is automatically numbered 0, Tue is 1, and so on.

A common early mistake is forgetting that an enum's value is actually a number (or string) underneath. Weekday.Mon looks like a name, but keeping in mind it's really the number 0 underneath helps avoid confusion.

In real projects, enum gets used for things like "day of the week," "order status," or "permission level" โ€” fields with a fixed set of possible values.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)