ProgramGuru

Type Narrowing Examples

Type Narrowing Examples — purpose and prerequisites

Type narrowing turns a union into a more specific type after a check TypeScript can understand. A discriminated union gives each variant a distinct tag, making an exhaustive switch predictable and safe.

What you’ll learn

  • Explain the purpose of type narrowing examples.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The kind field selects a variant, so user or amount is accessible in its corresponding branch. The never assignment flags missing cases if the union is extended.

Checking merely that an object exists does not establish which union variant it is; check the discriminant before property access.

Visualize the flow

Type Narrowing Examples — learning flowTagged union → Discriminant switch → Exhaustive resultType Narrowing Examples1Tagged union2Discriminant switch3Exhaustive resultRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

type Event = { kind: "login"; user: string } | { kind: "purchase"; amount: number };
function describe(event: Event): string {
  switch (event.kind) {
    case "login": return `Login: ${event.user}`;
    case "purchase": return `Purchase: ${event.amount}`;
    default: { const neverEvent: never = event; return neverEvent; }
  }
}
console.log(describe({ kind: "login", user: "Maya" }));
console.log(describe({ kind: "purchase", amount: 40 }));

Verified output

Login: Maya
Purchase: 40

Compile: npx tsc --strict --target es2022 --module commonjs type-narrowing-examples.ts; run: node type-narrowing-examples.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The kind field selects a variant, so user or amount is accessible in its corresponding branch. The never assignment flags missing cases if the union is extended.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Checking merely that an object exists does not establish which union variant it is; check the discriminant before property access.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Add a logout variant and observe the compile error until the switch handles it.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes type narrowing examples in the example?
Choose one answer. You can try again.

Answer and explanation

The kind field selects a variant, so user or amount is accessible in its corresponding branch.

The kind field selects a variant, so user or amount is accessible in its corresponding branch. The never assignment flags missing cases if the union is extended.

Key idea to remember

The kind field selects a variant, so user or amount is accessible in its corresponding branch. The never assignment flags missing cases if the union is extended.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Promise and Async Programs →