Literal types lock a value to an exact string/number/boolean. Union them for safe finite sets. Enums give a runtime object; union literals are tree-shakeable and often preferred in frontend code. Use const assertions (`as const`) to capture literal intent from arrays/objects.
type Status = "idle" | "loading" | "success" | "error";type Status = "idle" | "loading" | "success" | "error";
let status: Status = "loading";
// status = "done"; // ❌ not allowed
// Derive literal union from data
const buttons = ["primary", "secondary", "ghost"] as const;
type ButtonVariant = (typeof buttons)[number];
const variant: ButtonVariant = "ghost";
// Enum (runtime object)
enum Direction { Up = "UP", Down = "DOWN" }
const dir: Direction = Direction.Up;Understanding Literal Types & Enums is fundamental to mastering TypeScript. Practice with the examples above and experiment with variations to solidify your knowledge.