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'Exclude<Direction, 'UP'>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