Implement Pick, Omit and DeepReadonly: TypeScript Interview

Write Pick, Omit and DeepReadonly from scratch, a common TypeScript interview task, and explain each step: keyof, mapped and conditional types.

In this chapter

Interview: implement Pick, Omit and DeepReadonly

More on TypeScript interviews

Keep learning

Overview

chapters · typescript interviews Interview: implement Pick, Omit and DeepReadonly Write the utilities live, then explain each keyword you used. mapped types indexed access as remapping conditional types

1. The problem: the built-in Omit accepts typos

Omit and Pick are built-in utility types: tools that make a new object type from an old one. Omit<Dish, 'id'> means “Dish without its id field”. The interviewer opens with: “Why does this compile?” Built-in Omit = Omit = MyPick A base case is where the self-calling (recursion) stops. Functions: keep as is. Objects and arrays: recurse. Primitives (string, number, boolean…): stop. try it Cannot assign to 'name' because it is a read-only property. Cannot assign to 'price' because it is a read-only property. All three changes fail, as we want. dishes became a readonly array, and readonly arrays have no push method. That is why line 8 says push does not exist. Mapped types over arrays keep them arrays.

5. Bonus round: the modifiers

Modifiers are the ? (optional) and readonly marks on a field. A mapped type can add them, or remove them with a minus sign. add ? -? removes optional add readonly (no built-in) Mutable: remove readonly Record<K, V> K extends keyof any (any key name: string | number | symbol)

6. Explain it like this

Walk me through your Omit. keyof T gives the union of keys. Exclude (or as … never) drops K from that union. A mapped type walks the remaining keys. T[P] copies each value type. Mapping over keyof T keeps ? and readonly. Why does MyPick keep optional and readonly? Mapped types over keyof T (or K extends keyof T) are homomorphic. Homomorphic: they copy ? and readonly from T. Map over a plain union instead and modifiers are lost. homomorphic mapped types Why does the built-in Omit use keyof any for K? So it works in generic code where keyof T is not known yet. And on unions, where keyof gives only shared keys. Trade-off: typos pass. Wrap it as StrictOmit in app code. Omit on a union loses the variants. Why? keyof (A | B) is only the keys that A and B share, so the result is one merged object. Fix: distribute first, which means run Omit on A and on B separately. T extends any ? Omit<T, K> : never does that: a conditional type on a union runs once per member. distributive conditional types Where does DeepReadonly break down? Their methods still mutate. Special-case them, or use ReadonlyMap. Deep recursion on huge types can slow down type checking in the editor.

7. Scorecard

Constrained K extends keyof T Explained keyof, in, and T[P] Built Omit from Pick + Exclude Used as … never remapping Kept functions intact in DeepReadonly Mentioned homomorphic modifiers

Remember

Mapping a key to never with as removes it. DeepReadonly needs base cases: functions and primitives. ! Cheat code → Go deeper: mapped types, key remapping, build your own utilities, object utility types.

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.