TRT
Sign in
easyFoundationsdesign

How do Discriminated Unions and `assertNever(x: never)` eliminate illegal runtime states in TypeScript, and why should every external boundary (`JSON.parse`.

Explain set-theoretic narrowing, making illegal states unrepresentable, why `any` infects the call graph by disabling type checking, and how schema validators (Zod/Valibot) narrow `unknown` into typed discriminated unions.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read

A strong answer

A strong candidate frames How do Discriminated Unions and assertNever(x: never) eliminate illegal runtime states in TypeScript, and why should every external boundary (JSON.parse. around the exact memory layout, compiler semantics, and hardware execution model covered in [Set-Theoretic Structural Typing, Discriminated Unions, satisfies, and Exhaustive never]. Explain set-theoretic narrowing, making illegal states unrepresentable, why any infects the call graph by disabling type checking, and how schema validators (Zod/Valibot) narrow unknown into typed discriminated unions.

A payment webhook handler models transaction state using a single interface with optional fields: interface Payment { status: 'pending' | 'paid' | 'failed'; chargeId?: string; errorCode?: string }. Six months later, a developer adds 'refunded' to status and forgets to update one of the 14 if/else blocks in billing—causing refunded orders to silently fall through the else branch and ship merchandise for free!

When engineers encounter this bottleneck in production, a surface-level API description rarely explains what went wrong. In real workloads, every line of code executes against concrete hardware constraints: fixed-width memory cells, CPU cache lines, kernel descriptor tables, network socket buffers, or accelerator memory buses. Without a clear mental model of TypeScript structural typing, discriminated unions, and exhaustive never narrowing, debugging degrades into guesswork—tuning timeouts, adding retries, or scaling instances blindly while the underlying architectural flaw remains untouched.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Replace error-prone optional-field bags with set-theoretic Discriminated Unions that make illegal states unrepresentable.
  • Mastery of core invariant: Enforce 100% compile-time exhaustiveness across all switch statements using assertNever(x: never).
  • Mastery of core invariant: Use satisfies to validate constraints without widening literals and unknown to guard untrusted I/O boundaries.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Why does TypeScript allow an object variable `const p = { x: 1, y: 2, z: 3 }; const p2: Point2D = p;` to pass, while rejecting a direct object literal `const p2: Point2D = { x: 1, y: 2, z: 3 };`.
  2. 02Why is `useUnknownInCatchVariables` enabled by default under `--strict`?

Not started

Sign in to save your learning progress.

Sign in to save