Nested Property Path Generation
What you'll learn: How to automatically generate type-safe dot-notation paths like "user.address.city" from deeply nested object types.
The Core Challenge
When working with nested objects, you often need to reference properties using string paths — for configuration systems, form libraries, data access utilities, or state management. Writing these paths manually is error-prone and loses type safety.
The Solution: Recursive Template Literals
By combining mapped types, template literals, and conditional types, you can recursively traverse an object structure and generate all valid property paths automatically:
type NestedPaths<T> = T extends object
? {
[K in keyof T]: K extends string
? T[K] extends object
? K | `${K}.${NestedPaths<T[K]>}`
: K
: never
}[keyof T]
: never;
type User = {
name: string;
address: {
street: string;
city: string;
};
};
type UserPaths = NestedPaths<User>;
// Result: "name" | "address" | "address.street" | "address.city"
How It Works
The type recursively iterates over each property. For each key, if the value is an object, it creates both the standalone key ("address") and concatenated paths using template literals ("address.street"). The recursion continues until it hits non-object values (leaves).
The mapped type transforms all keys, and the indexed access [keyof T] at the end collects all generated paths into a union.
Key Takeaway: Recursive mapped types with template literals can automatically generate type-safe dot-notation paths for deeply nested objects, enabling fully typed utilities for property access, validation, and manipulation.