Let's imagine a unique tree structure:
type Tree = {
a: {
b: {
c: "d"
}
}
}
Now, consider wanting to transform each node in the tree into a union type, say "f"
. This means the updated tree would look like this:
type Tree = {
a: {
b: {
c: "d" | "f"
} | "f"
} | "f"
} | "f"
The objective is to create the above type using a utility called
DescendantsUnionedWith<T, F>
, where T
represents the original Tree
type and F
stands for "f"
.
To tackle this issue, I initially developed a generic type named ChildrenUnionedWith
:
export type ChildrenUnionedWith<O extends object, T> = {
[K in keyof O]: O[K] | T;
};
Following that, my attempt was to build a recursive deep tree type:
export type DescendantsUnionedWith<Tree extends object, T> = Tree extends object
? ChildrenUnionedWith<DescendantsUnionedWith<Tree, T>, T>
: Tree | T;
Unfortunately, I encountered a circularity error when trying to implement this approach.
If anyone has insights on how I can successfully implement the DescendantsUnionedWith
utility type, your help would be greatly appreciated!
Thank you!