Given an object structure like the one below:
type IObject = { id: string, path: string, children?: IObject[] }
const tree = [
{
id: 'obj1' as const,
path: 'path1' as const,
children: [
{
id: 'obj2' as const,
path: ':pathParam' as const,
children: [
{
id: 'obj3' as const,
path: 'path3' as const
}
]
},
{
id: 'obj4' as const,
path: 'path4' as const,
children: [
{
id: 'obj5' as const,
path: 'path5' as const
}
]
}
]
}
];
I am attempting to retrieve the type of a nested object AND an accumulation of the path
fragments leading to that nested type. (assuming unique IDs across the object)
example
type ok = FindAndTrackDeepPath<'obj2', typeof tree>['fullPath'];
// type ok = "/path1/:pathParam"
I have tried a solution but it's not working as expected. When recursion occurs, it creates a union of all options at each level, causing the issue demonstrated in the second example.
export type FindAndTrackDeepPath<
ID,
T extends IObject[],
depth extends number = 1,
path extends string = '',
maxDepth extends number = 10
> = ...
The problem arises when attempting to filter out unwanted descendents while recursing. This leads to incorrect paths being generated.
Upon research, I found similar questions here and here, but they extract all possible paths instead of specific ones that match particular criteria.
If you can provide any assistance, I'd greatly appreciate it. Is the desired outcome achievable?