Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 7 of 9

Literal Types, `as const`, and Enums

Literal types narrow a type to a specific value. `as const` freezes an object or array so every value becomes its literal type. Enums give named constants - but TypeScript's `const enum` and union-of-literals idiom are often preferable to regular enums for tree-shaking and clarity.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Literal Types, as const, and Enum Patterns

Literal Types, as const, and Enum Patterns
// ── Literal types ────────────────────────────────────────────
let direction: "left" | "right" | "up" | "down";
direction = "left";   // ✅
direction = "north";  // ❌ Error

type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";

function request(url: string, method: HttpMethod): void { /* ... */ }
request("/api/tasks", "GET");    // ✅
request("/api/tasks", "FETCH"); // ❌ Error

// ── as const - literal inference preservation ──────────────────
// Without as const - TypeScript widens to string[]
const directions = ["left", "right", "up", "down"];
// Type: string[]  ← too wide!

// With as const - every value frozen to its literal type
const DIRECTIONS = ["left", "right", "up", "down"] as const;
// Type: readonly ["left", "right", "up", "down"]

type Direction = (typeof DIRECTIONS)[number];
// Type: "left" | "right" | "up" | "down"  ← derived from array!

// Object as const - all values become readonly literals
const CONFIG = {
  apiUrl: "https://api.example.com",
  timeout: 5000,
  retries: 3,
} as const;
// CONFIG.apiUrl is typed as "https://api.example.com", not string
// CONFIG.timeout is typed as 5000, not number

// ── Enums - named constants ────────────────────────────────────
// Regular enum - compiled into a JS object (runtime cost)
enum TaskStatus {
  Draft     = "draft",
  Published = "published",
  Archived  = "archived",
}
const s: TaskStatus = TaskStatus.Draft;  // "draft"

// const enum - erased at compile time (replaced inline)
const enum Priority {
  Low    = 1,
  Medium = 2,
  High   = 3,
}
const p = Priority.High; // compiles to: const p = 3;

// ── Union-of-literals - preferred in modern TypeScript ─────────
// Equivalent to enum but more flexible and tree-shakable:
type TaskStatusType = "draft" | "published" | "archived";
const TASK_STATUSES = ["draft", "published", "archived"] as const;
type TaskStatusFromArray = (typeof TASK_STATUSES)[number]; // same type

// ── When to use enum vs union-of-literals ─────────────────────
// Enum:              when you need reverse mapping (numeric enums only)
//                   OR when working with a framework expecting enum
// Union-of-literals: in most cases - simpler, no runtime object, tree-shakable

Common Mistakes

  • Using regular enum when only string values are needed - the runtime JS object generated by regular enums adds unnecessary bundle weight. Prefer `const enum` or union-of-literals.
  • Forgetting `as const` on config objects - without it, values widen to `string`/`number` and you lose the literal precision needed for type narrowing.
  • Deriving types from enums with `typeof MyEnum` - this gives you the type of the enum object itself (the constructor), not the union of its values. Use `keyof typeof MyEnum` for keys.

Tip

Tip

Practice Literal Types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Literal Types, `as const`, and Enums

Practice Task

Note

Practice Task - (1) Write a working example of Literal Types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Literal Types is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • Literal types narrow a type to a specific value.
  • Using regular enum when only string values are needed - the runtime JS object generated by regular enums adds unnecessary bundle weight. Prefer `const enum` or union-of-literals.
  • Forgetting `as const` on config objects - without it, values widen to `string`/`number` and you lose the literal precision needed for type narrowing.
  • Deriving types from enums with `typeof MyEnum` - this gives you the type of the enum object itself (the constructor), not the union of its values. Use `keyof typeof MyEnum` for keys.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done