Advanced Type Patterns
Type Aliases and Literal Unions
Type aliases can name a set of literal values and make branch choices explicit.
literal union
A literal union type allows only specific values such as `"draft"` or `"published"`.
Type Aliases and Literal Unions
aliases.ts
Replay: real traced execution (multi-file project)
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 = "review";
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 = "draft";
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 = "published";
const label: string = statusLabel(status);
console.log(`${status}=${label}`);
status ← review
10 return "hidden";11}1213const status→ review: Status = "review"; //@status="draft", "published"14const label: string = statusLabel(statusreview);function statusLabel(status: Status): string
1type Status = "draft" | "review" | "published";23function statusLabel(statusreview: Status): string {4 if (status === "published") {if (status === "review")
5 return "visible";6}7if (statusreview === "review") {8 return "checking";9}label ← checking
13const status: Status = "review"; //@status="draft", "published"14const label→ checking: string = statusLabel(statusreview);1516console.log(`${statusreview}=${labelchecking}`);outputreview=checking
status ← draft
10 return "hidden";11}1213const status→ draft: Status = "draft";14const label: string = statusLabel(statusdraft);function statusLabel(status: Status): string
1type Status = "draft" | "review" | "published";23function statusLabel(statusdraft: Status): string {4 if (status === "published") {5 return "visible";6 }7 if (status === "review") {8 return "checking";9 }10 return "hidden";11}label ← hidden
13const status: Status = "draft";14const label→ hidden: string = statusLabel(statusdraft);1516console.log(`${statusdraft}=${labelhidden}`);outputdraft=hidden
status ← published
10 return "hidden";11}1213const status→ published: Status = "published";14const label: string = statusLabel(statuspublished);function statusLabel(status: Status): string
1type Status = "draft" | "review" | "published";23function statusLabel(statuspublished: Status): string {4 if (status === "published") {if (status === "published")
3function statusLabel(status: Status): string {4 if (statuspublished === "published") {5 return "visible";6 }label ← visible
13const status: Status = "published";14const label→ visible: string = statusLabel(statuspublished);1516console.log(`${statuspublished}=${labelvisible}`);outputpublished=visible