Day 39 of 100: Enums vs Literal Types — When Each Wins
Day 39 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: TypeScript. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).
🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.
Enums group related constants. Literal unions often do it better. Know both; choose deliberately.
Contents
Enum Syntax
enum Env {
Local = 'http://localhost:3000',
Staging = 'https://staging.example.com',
Prod = 'https://example.com',
}
const target: Env = Env.Staging;
enum HttpStatus { OK = 200, Created = 201, NotFound = 404 }
HttpStatus.Created; // 201
🚀 Level Up Your Playwright
From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.
Literal Union Alternative
type Browser = 'chromium' | 'firefox' | 'webkit';
const b: Browser = 'firefox';
// const-object pattern — enum ergonomics, zero runtime cost:
const ENVS = {
local: 'http://localhost:3000',
staging: 'https://staging.example.com',
} as const;
type EnvKey = keyof typeof ENVS; // 'local' | 'staging'
Decision Table
- String literal unions: default choice — zero runtime code, perfect inference
- Enums: when you need a runtime value map (env → URL) referenced by name
- as const objects: best of both — values + derived types
- Avoid numeric auto-enums — opaque values in failure logs
Tomorrow (Day 40): Typing functions — params, returns, async signatures.
🎓 Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
