Literal Types
Prerequisites: Union types
A literal type is an exact value used as a type — most useful combined with a union, to restrict a value to a fixed set of options.
type Direction = "up" | "down" | "left" | "right";
function move(direction: Direction) {
console.log(`Moving ${direction}`);
}
move("up"); // fine
move("sideways"); // Argument of type '"sideways"' is not assignable...
This catches typos in string "modes" or "statuses" at compile time, instead of discovering a mistyped status string only at runtime.