TypeScript1 min read

TypeScript Type Narrowing & Discriminated Unions: Interview Questions & Code

Master TypeScript type narrowing techniques, discriminated unions, type guards (typeof, instanceof, in, is), and exhaustiveness checking using never.

Pairlet Team•Published: •Updated:

TypeScript's powerful type inference engine relies on Type Narrowing—the process of refining a broad type into a more specific type within a conditional code block.

1. Type Guards: typeof, instanceof, and in

TYPESCRIPT
function formatInput(val: string | number | Date) {
  if (typeof val === "string") {
    return val.toUpperCase(); // Narrowed to string
  }
  if (val instanceof Date) {
    return val.toISOString(); // Narrowed to Date
  }
  return val.toFixed(2); // Narrowed to number
}

2. Custom Type Guard Functions (is Keyword)

TYPESCRIPT
interface User { id: string; name: string; }
interface Admin extends User { permissions: string[]; }

function isAdmin(user: User): user is Admin {
  return "permissions" in user && Array.isArray((user as Admin).permissions);
}

3. Discriminated Unions & Exhaustiveness Checking

A Discriminated Union is a union type where every member shares a common literal property (the discriminant).

TYPESCRIPT
type NetworkState =
  | { status: "idle" }
  | { status: "loading" }
  | { status: "success"; data: string[] }
  | { status: "error"; error: Error };

function renderUI(state: NetworkState): string {
  switch (state.status) {
    case "idle": return "Ready";
    case "loading": return "Loading...";
    case "success": return `Loaded ${state.data.length} items`;
    case "error": return `Error: ${state.error.message}`;
    default: {
      const _exhaustiveCheck: never = state;
      return _exhaustiveCheck;
    }
  }
}

From Type Knowledge to Code Review

TypeScript interviews become more realistic when candidates must maintain and review an existing typed codebase. Ask them to inspect Bad TypeScript Design, identify unsafe assertions or weak interfaces, and propose a safer boundary. You can also use Authorization & Security Bug to test whether type safety is connected to runtime authorization, or Overengineered Code to discuss maintainability and simplicity.

For a broader interviewer framework, read the TypeScript code review best practices guide and browse the code review exercise library.

Practice TypeScript Interviews Live

Evaluate candidate TypeScript type safety live in a collaborative editor. Create a Pairlet Room.

Frequently Asked Questions

How do you test TypeScript type narrowing in an interview?

Give the candidate a union with a shared discriminant, ask them to implement a type guard, and then require an exhaustive switch. A good follow-up is to remove one branch and see whether the compiler catches the missing case.

Practice Relevant Coding Problems
Practice Live Coding

Conduct Live Coding Interviews in the Browser

No candidate sign-up required. Create a room, share the link, and code together in real time with shared code execution.

Related Articles