HardPro challengeJavaScriptTypeScript

Deep Readonly (TypeScript)

TypeScriptTypesUtility Types

Implement the DeepReadonly<T> utility type that recursively makes every
property (and nested property) of an object type readonly.

Then implement solve(obj) which accepts a DeepReadonly<Config> object
and returns a summary string proving the type is correct at runtime.

Type requirements

  • Primitives stay as-is.
  • Arrays become ReadonlyArray<DeepReadonly<Item>>.
  • Objects get readonly on every property, recursively.
  • Functions are left untouched.

The test suite checks the runtime output of solve. The type challenge
is enforced by TypeScript compilation.

Sample tests

Test #1Standard config object
Input: [{"db":{"name":"mydb","replicas":["r1","r2"]},"host":"localhost","port":5432}]
Output: "localhost:5432/mydb(2)"
Test #2No replicas
Input: [{"db":{"name":"appdb","replicas":[]},"host":"prod.db","port":3306}]
Output: "prod.db:3306/appdb(0)"
Test #3Three replicas
Input: [{"db":{"name":"mongo","replicas":["a","b","c"]},"host":"10.0.0.1","port":27017}]
Output: "10.0.0.1:27017/mongo(3)"
Test #4Minimal values
Input: [{"db":{"name":"y","replicas":["r"]},"host":"x","port":0}]
Output: "x:0/y(1)"
Test #5Five replicas
Input: [{"db":{"name":"prod","replicas":["r1","r2","r3","r4","r5"]},"host":"a.b.c","port":65535}]
Output: "a.b.c:65535/prod(5)"