Advanced Type Patterns
Type Aliases and Literal Unions
Type aliases can name a set of literal values and make branch choices explicit.
Type Aliases and Literal Unions
aliases.ts
type Status = "draft" | "review" | "published";
function statusLabel(status: Status): string {
if (status === "published") {
return "visible";
}
if (status === "review") {
return "checking";
}
return "hidden";
}
const status: Status = ;
const label: string = statusLabel(status);
console.log(`${status}=${label}`);
type Status = "draft" | "review" | "published";
function statusLabel(status: Status): string {
if (status === "published") {
return "visible";
}
if (status === "review") {
return "checking";
}
return "hidden";
}
const status: Status = ;
const label: string = statusLabel(status);
console.log(`${status}=${label}`);
type Status = "draft" | "review" | "published";
function statusLabel(status: Status): string {
if (status === "published") {
return "visible";
}
if (status === "review") {
return "checking";
}
return "hidden";
}
const status: Status = ;
const label: string = statusLabel(status);
console.log(`${status}=${label}`);
literal union
A literal union type allows only specific values such as `"draft"` or `"published"`.