Type-safe function to merge path segments based on an array of keys

The following Paths type represents a number of url paths broken up by path segment.

type PathSegment = {
    path: string;
    children: Paths;
}

type Paths = Record<string, PathSegment | string>;

The following is an example object matching this type:

const paths: Paths = {
    home: "/",
    profile: "/profile",
    settings: {
        path: "/settings",
        children: {
            general: "/general",
            account: "/account",
        }
    }
}

Is it possible to create a function that merges path segments together in a type-safe way by providing the keys of the paths that should be merged? For example, given the paths object above, I could do something like:

const accountSettingsPath = mergePathSegments(paths, ["settings", "account"]);
console.log(accountSettingsPath); // outputs "/settings/account"

The keys passed in via the array should be known at compile-time so that a typo can’t be made. And the array can’t be bigger than the number of keys that are available. Is something like this possible?