Literal Types
What
Literal types allow you to specify that a variable must contain an exact value, rather than just any value of a broad type like string or number.
How
You use the literal value itself as the type definition:
// direction can ONLY be "north"
function move(direction: "north") {
// ...
}
move("north"); // OK
move("south"); // Error: Argument of type '"south"' is not assignable to parameter of type '"north"'.
Why
Literal types are far more powerful than broad types when you have a known, finite set of valid values. They allow the TypeScript compiler to catch errors where you might pass an invalid string that would otherwise be technically “valid” in a standard string type.
Summary
Literal types provide a lightweight way to enforce exact values without the overhead of more complex structures.