Sub topic 1
Literal Types: Types That Hold Exactly One Value
Prerequisites
You should know basic TypeScript types such as string, number, and boolean. You should also be comfortable writing functions, object literals, and switch statements in JavaScript.
What a Literal Type Is
A literal type describes one exact value rather than a broad category of values. For example, string allows any string, but "draft" allows only the string "draft".
type Status = "draft";
type Port = 8080;
type Enabled = true;
const status: Status = "draft";
const port: Port = 8080;
const enabled: Enabled = true;
console.log(status, port, enabled);The value and its type are related but different:
"draft"is a string value and can also be a string literal type.8080is a number value and can also be a numeric literal type.trueis a boolean value and can also be a boolean literal type.
A variable with type string can hold "draft", "published", or any other string. A variable with type "draft" can hold only "draft".
function publish(status: "draft" | "published"): string {
return status === "draft" ? "Ready to publish" : "Already published";
}
console.log(publish("draft"));
console.log(publish("published"));The | symbol creates a union type. A union type allows one value from a fixed set. The function accepts the two listed strings, but a different string would be rejected by the compiler.
Why Exact Values Matter
Literal types are useful when a program has a small, known set of valid choices. They make invalid states visible while you are writing code instead of waiting for a runtime failure.
type Direction = "up" | "down" | "left" | "right";
function move(direction: Direction): void {
console.log(`Moving ${direction}`);
}
move("up");
move("left");A call such as move("diagonal") would produce a type error because "diagonal" is not part of Direction.
Literal types also describe numeric choices:
type HttpSuccess = 200 | 201 | 204;
function isSuccess(code: HttpSuccess): boolean {
return code === 200 || code === 201 || code === 204;
}
console.log(isSuccess(201));Boolean literal types are useful when a type must represent one particular state:
type AlwaysEnabled = true;
const featureEnabled: AlwaysEnabled = true;
console.log(featureEnabled);A boolean literal type is narrower than boolean. A value of type boolean may be either true or false, while a value of type true can only be true.
Literal Types in Object Properties
Literal types often act as labels in object types. A label identifies which variant of data an object contains.
type Circle = {
kind: "circle";
radius: number;
};
type Square = {
kind: "square";
size: number;
};
const shape: Circle = {
kind: "circle",
radius: 5,
};
console.log(shape.kind, shape.radius);The kind property is more informative than a plain string property. TypeScript can use its exact value to understand that this object is a circle. This pattern is called a discriminated union when several object types share a literal property such as kind.
Common Mistake: Confusing Values and Types
A literal value appears in an expression:
const mode = "dark";A literal type appears in a type position:
let mode: "dark" = "dark";The annotation restricts mode to one value. Without an annotation, TypeScript may infer a broader type depending on whether the variable can change. Understanding that inference is the next step when working with literal types.
Exercise
What is the difference between string and "ready" as types?
Show answer
string allows any string value, while "ready" allows only the exact string "ready".