Merge Overrides into Defaults at Any Depth

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Export a type DeepPartial<T> that makes every property optional at every depth of nested objects, while arrays stay whole (an override replaces an array, it doesn't patch it). Then write mergeDeep(defaults, overrides): return a new object where nested plain objects are merged key by key, arrays and other values in overrides replace the defaults, and undefined overrides are ignored, leaving defaults unchanged. Type overrides as DeepPartial of the defaults, so it may leave out anything but can't give a value of the wrong type or a key the defaults don't have, and type the result as the defaults' full type.

Your code must pass

  • Merges nested objects key by key
  • Replaces arrays and plain values whole
  • Ignores undefined overrides
  • Leaves defaults unchanged
  • Has no type errors
  • Overrides may leave out anything at any depth
  • Rejects overrides of the wrong type
  • Returns the full type