Literal Types
TS · Type Aliasessyntax
type Name = "exactValue" | "anotherValue";
type Name = 0 | 1 | 2;example
type Direction = "north" | "south" | "east" | "west";
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
function move(direction: Direction, steps: number) {
console.log(`Moving ${direction} by ${steps}`);
}
move("north", 3); // OK
// move("up", 3); // Error: Argument of type '"up"' is not assignableoutput
// Literal types restrict a value to an exact set of allowed valuesNote Literal types turn strings, numbers, or booleans into specific-value types. Combined with unions, they create powerful enumerations without the overhead of enum. const assertions (as const) automatically infer literal types.