I want to write generic recursive function like below
function withChildren<
T extends {
id?: string;
parentId?: string;
},
TWithChild extends T & {
children: TWithChild[];
}
>(parentItem: T, items: T[]): TWithChild {
const children = items.filter((ba) => ba.parentId === parentItem.id);
return {
...parentItem,
children: children.map((child) => withChildren(child, items)),
};
}
but typescript throw an error
Type 'T & { children: (T extends BusinessAreaWithAccess ? BusinessAreaWithChildrenAndAccess : BusinessAreaWithChildren)[]; }' is not assignable to type 'T extends BusinessAreaWithAccess ? BusinessAreaWithChildrenAndAccess : BusinessAreaWithChildren'
i have search for the error, but still not found any solution