Freeze Nested Data, Types Included

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

The problem

  • medium
  • Code
  • Pro

Export a type DeepReadonly<T> that makes every property read-only at every depth, arrays included (functions stay as they are). Then write freezeDeep(obj): freeze obj and every object and array inside it, and return it typed as DeepReadonly of its input, so that writing to it anywhere is a type error.

Your code must pass

  • Freezes every nested object and array
  • Returns the same object
  • Has no type errors
  • Rejects writes at any depth
  • Rejects changing a nested array
  • Reading stays fully typed