Lab 05: Enums & Literal Types
Objective
Time
Prerequisites
Tools
Lab Instructions
Step 1: Numeric Enums
enum Direction { North, South, East, West } // 0, 1, 2, 3
enum HttpStatus {
OK = 200, Created = 201, NoContent = 204,
BadRequest = 400, Unauthorized = 401, NotFound = 404,
InternalError = 500,
}
function describeStatus(code: HttpStatus): string {
switch (code) {
case HttpStatus.OK: return "✅ Success";
case HttpStatus.Created: return "✅ Resource created";
case HttpStatus.NotFound: return "❌ Not found";
case HttpStatus.InternalError: return "💥 Server error";
default: return `HTTP ${code}`;
}
}
console.log(describeStatus(HttpStatus.OK));
console.log(describeStatus(HttpStatus.NotFound));
console.log("Direction.North =", Direction.North);
console.log("Direction[0] =", Direction[0]); // reverse mappingStep 2: Const Enums & Literal Types
Step 3: Discriminated Unions as State Machines
Steps 4–8: Enum Utilities, Object Enums, Exhaustiveness, Flags, Capstone
Summary
Further Reading
Last updated
