Interview: the new status nobody handled

The API added a state and the UI showed blank. Make the compiler catch it next time. A free TypeScript lesson with examples.

More on TypeScript interviews

Keep learning

Overview

chapters · typescript interviews · ticket 2 “Refunded orders show a blank status” The API added a state and the UI showed nothing. Make the compiler catch it next time. union of literals switch narrowing implicit undefined Record<Union, …> Refunded orders render an empty status pill. No error anywhere. Exhaustive check with never, so a new member fails the build.

Status pill blank on refunded orders

Customers with a refund see an empty grey pill. tsc passes. No console errors. tsc errors orders affected states handled

statusLabel.ts · compiles, returns undefined

→ 'Order placed' | 'In the kitchen' | 'Enjoy!' | undefined

Why did nothing fail?

What type is label? 'Order placed' | 'In the kitchen' | 'Enjoy!' 'Order placed' | 'In the kitchen' | 'Enjoy!' | undefined

From blank pill to missing case

What value reaches the pill? log statusLabel(status) undefined for 'refunded'. Is the type up to date? read types.ts Yes. 'refunded' is in the union. So why no error? Return type is inferred, and it quietly includes undefined. How do I make a missing case a compile error? Are there other switches on status? grep 'switch (status' Two more. Same fix, or a Record lookup. Argument of type 'string' is not assignable to parameter of type 'never'. satisfies never Type 'string' does not satisfy the expected type 'never'. Record map Record<OrderStatus, string> means “an object with one string for every OrderStatus key”. The object has only three keys, so TS names the missing one. Best for pure lookups.

How the type of s shrinks inside the switch

'placed' | 'cooking' | 'delivered' | 'refunded' after case 'placed' 'cooking' | 'delivered' | 'refunded' after case 'cooking' 'delivered' | 'refunded' after case 'delivered' in default: 'refunded' is not never ✕ error at assertNever(s)

Exhaustive, everywhere, with a runtime fallback

Can lint catch this instead? Yes: typescript-eslint switch-exhaustiveness-check. It flags a switch on a union that misses members. knows the rule

What should the default branch do?

A status the code does not know arrives. What happens? never-check at compile time + UI fallback Catches at build User-facing risk assertNever that throws, everywhere Great for backend logic. In the UI, an old client crashes on a new status. Same bug as the ticket, just on purpose. Nothing ever fails.

Did you show these?

Spotted the inferred | undefined Explained narrowing down to never Used assertNever or satisfies never Chose Record for pure lookups Split compile-time from runtime handling Searched for other switches on the same union

Remember

After every case, the value narrows to never. Assign the leftover to never to make missing cases errors. Record<Union, V> forces a value for every member. Exhaustive at compile time, tolerant at runtime. ! Cheat code → Go deeper: exhaustive checks with never, discriminated unions, narrowing pitfalls, enum or union?.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.