EasyPro challengeJavaScriptTypeScript
as const — Enum-like String Unions
TypeScriptTypes
TypeScript's as const turns an object into a fully immutable, literal-typed structure — the idiomatic alternative to TypeScript enums:
// Without as const — values are widened to string:
const Direction = { Up: 'UP', Down: 'DOWN' };
// Direction.Up is inferred as string — too wide!
// With as const — values are literal types:
const Direction = { Up: 'UP', Down: 'DOWN' } as const;
// Direction.Up is 'UP' — exactly right ✅
// Derive the union type automatically:
type Direction = typeof Direction[keyof typeof Direction];
// → 'UP' | 'DOWN'Why prefer this over TypeScript enums?
- No runtime overhead — just a plain object
- No double declaration — one source of truth
- Works with union type utilities —
Exclude<Direction, 'UP'> - No numeric enum footguns — values are exactly what you write
Implement
Two predefined const objects:
Direction:{ Up: 'UP', Down: 'DOWN', Left: 'LEFT', Right: 'RIGHT' }HttpStatus:{ Ok: 200, NotFound: 404, Unauthorized: 401, Error: 500 }
Implement isDirection(x): x is Direction and isHttpStatus(x): x is HttpStatus using Object.values().
Sample tests
Test #1UP is a valid Direction
Input: ["Direction","UP"]
Output: true
Test #2DOWN is a valid Direction
Input: ["Direction","DOWN"]
Output: true
Test #3lowercase "up" is not valid (case-sensitive)
Input: ["Direction","up"]
Output: false
Test #4NORTH is not in the Direction enum
Input: ["Direction","NORTH"]
Output: false
Test #5200 is a valid HttpStatus
Input: ["HttpStatus",200]
Output: true
Test #6404 is a valid HttpStatus
Input: ["HttpStatus",404]
Output: true
Test #7418 is not in HttpStatus
Input: ["HttpStatus",418]
Output: false