My current challenge involves a recursive function that operates on an object with a generic interface. This function essentially traverses the object and creates a new one with a nearly identical interface, except for the leaf nodes which are transformed into numbers. Even though the function successfully generates this new object, I am struggling to properly assign the correct type to it.
For example:
interface IOriginal
{
prop1: string,
prop2: number,
prop3: {
prop1: boolean,
prop2: {
prop1: number
},
prop3: string
}
}
const input : IOriginal = {
prop1: "somestring",
prop2: 5,
prop3: {
prop1: true,
prop2: {
prop1: 2
},
prop3: "otherstring"
}
}
function traverse<T>(obj: T)
{
/* ... map the object ... */
return mappedObj
}
const output = traverse(input)
The desired interface of the output (mapped object) should be as follows:
interface IOutput
{
prop1: number,
prop2: number,
prop3: {
prop1: number,
prop2: {
prop1: number
},
prop3: number
}
}
I have found that using mapped types only allows me to manipulate the first level of depth, if my explanation makes sense.