What Are Literal Types?
You'll learn how TypeScript can treat specific values—like "hello" or 42—as their own distinct types, not just generic categories like string or number.
Beyond Categories to Exact Values
So far, you've worked with broad type categories: string accepts any text, number accepts any numeric value, and boolean accepts true or false. But TypeScript goes further—it can treat specific values as types themselves.
A literal type is a type that represents one exact value. Instead of saying "this variable is any string," you can say "this variable is exactly the string 'success'." Instead of "any number," you can specify "exactly 0."
How Literal Types Work
When you use const to declare a variable with a primitive value, TypeScript infers a literal type:
const status = "success";
// Type inferred: "success" (not string)
const code = 404;
// Type inferred: 404 (not number)
const isReady = true;
// Type inferred: true (not boolean)
Notice TypeScript didn't say status is type string—it said status is type "success". That's a literal type. The variable can only ever hold that exact value.
With let, TypeScript widens to the broader category because the value can change:
let status = "success";
// Type inferred: string (widened because it's reassignable)
You can explicitly annotate literal types too:
let direction: "north" | "south" | "east" | "west";
direction = "north"; // ✓ valid
direction = "up"; // ✗ error: not one of the four allowed literals
Key Takeaway: Literal types represent exact, specific values rather than broad categories, giving you precise control over what values are allowed and catching mistakes that generic types like string or number would miss.