Type alias Paths<T, Prev>

Paths<T, Prev>: {
    [K in keyof T & string]: `${Prev}${K}` | (T[K] extends object
        ? Paths<T[K], `${Prev}${K}.`>
        : never)
}[keyof T & string]

Generates all possible dot-separated key paths from a nested object type. Returns a union of all valid key paths as string literals.

Type Parameters

  • T

    The object to extract paths from

  • Prev extends string = ""

    The accumulated path (used for recursion)

Example

type Obj = {
user: {
profile: {
name: string;
};
};
age: number;
};

type P = Paths<Obj>;
// Result:
// 'user'
// 'user.profile'
// 'user.profile.name'
// 'age'