What you're building
A deepGet utility that drills into a nested object using a dot-separated
path string and returns the value β or undefined if any segment is missing β
without ever throwing.
This is the runtime logic behind libraries like Lodash _.get and many config
readers. The bonus: we add TypeScript overloads so callers get the correct
return type for known paths.
Requirements
Write deepGet(obj: Record<string, unknown>, path: string): unknown:
pathis a dot-separated key string, e.g."user.address.city".- Split on
"."and walk the object one key at a time. - If any intermediate value is
null,undefined, or not an object, returnundefinedimmediately (do not throw). - An empty path string returns
undefined. - Single-segment paths (no dot) return the top-level field value.
Stretch goals
- Implement array index support so
"items.0.name"works on arrays. - Add a typed overload:
deepGet<T>(obj: T, path: P): DeepValue<T, P>using template-literal types and recursive conditional types.
What this proves
You can implement safe, defensive traversal logic and reason about runtime shapes
of unknown data β essential for working with API responses, config objects, and
any JSON.parse output.
β‘ 60 XP
solution.tsTranspiled & run in your browser
Loading editorβ¦
Tests4 to pass
retrieves a top-level key
drills two levels deep
returns undefined for missing key
returns undefined when intermediate is not an object
Run your code to check these.