Skip to the solution
HardFrontend11 min read

How would you build a DeepPartial type?

857 views

The problem

Question presented to candidate: "Partial<T> only makes the top level optional. How would you build a DeepPartial<T> that makes every nested property optional β€” and what would you watch out for?"

What a strong answer should cover:

  • The basic idea: a mapped type with ? that recurses into object-typed properties
  • Why the one-liner T[K] extends object ? DeepPartial<T[K]> : T[K] breaks: X | null is not distributed so it is not recursed into, and Dates and functions turn into bags of optional members ({} is accepted, functions stop being callable)
  • A robust version: leaf types (primitives, Date, RegExp, functions) pass through, a naked T distributes over unions, arrays and tuples keep their shape, Map and Set handled explicitly
  • Typical use: config overrides and test fixtures, merged into defaults by a deep-merge function whose behaviour matches the type (arrays replace, undefined keeps the default)
  • Caveats: explicit undefined is allowed under plain strict (exactOptionalPropertyTypes changes that), and inside generic code DeepPartial<T> stays unresolved

Clarifying questions expected:

  • Should arrays become partial element by element, or be replaced as a whole?
  • Which types are leaves for this codebase (dates, class instances, branded types)?

Code / implementation expected: Yes β€” write DeepPartial<T>, show where the naive version fails, and use it in a typed merge.

  • partial-deep

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read Β· 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptThe naive DeepPartial, its three bugs and a robust version
TypeScriptTyped config overrides with a matching deepMerge

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 67 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How do you type and compose multiple disposables with `DisposableStack` and `AsyncDisposableStack`?

Continue