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

status
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}`);
  1. status ← review

    10    return "hidden";11}1213const status→ review: Status = "review";  //@status="draft", "published"14const label: string = statusLabel(statusreview);
  2. function statusLabel(status: Status): string

    1type Status = "draft" | "review" | "published";23function statusLabel(statusreview: Status): string {4    if (status === "published") {
  3. if (status === "review")

    5    return "visible";6}7if (statusreview === "review") {8    return "checking";9}
  4. label ← checking

    13const status: Status = "review";  //@status="draft", "published"14const label→ checking: string = statusLabel(statusreview);1516console.log(`${statusreview}=${labelchecking}`);
    outputreview=checking
  1. status ← draft

    10    return "hidden";11}1213const status→ draft: Status = "draft";14const label: string = statusLabel(statusdraft);
  2. 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}
  3. label ← hidden

    13const status: Status = "draft";14const label→ hidden: string = statusLabel(statusdraft);1516console.log(`${statusdraft}=${labelhidden}`);
    outputdraft=hidden
  1. status ← published

    10    return "hidden";11}1213const status→ published: Status = "published";14const label: string = statusLabel(statuspublished);
  2. function statusLabel(status: Status): string

    1type Status = "draft" | "review" | "published";23function statusLabel(statuspublished: Status): string {4    if (status === "published") {
  3. if (status === "published")

    3function statusLabel(status: Status): string {4    if (statuspublished === "published") {5        return "visible";6    }
  4. label ← visible

    13const status: Status = "published";14const label→ visible: string = statusLabel(statuspublished);1516console.log(`${statuspublished}=${labelvisible}`);
    outputpublished=visible