- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
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.
๐งช 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).
