I have a function that recursively removes a particular property from all objects. If we write it in Javascript, it would be:
function removeRecursive(value) {
if (Array.isArray(value)) {
return value.map(removeRecursive);
}
if (typeof value === "object") {
return Object.keys(value)
.filter(key => key !== "remove")
.reduce((acc, key) => {
acc[key] = removeRecursive(value[key]);
return acc;
}, {});
}
return value;
};
For example:
removeRecursive({
foo: {
bar: 'foo.bar',
remove: 'this'
},
remove: 'also-this',
list: [{
remove: 'this-as-well',
baz: 'baz'
}]
})
Would result in:
{
"foo": {
"bar": "foo.bar"
},
"list": [
{
"baz": "baz"
}
]
}
Any idea how I can type this? It seems like I should have enough information to be able to return a new type that somehow recursively returns the same type with the remove
property omitted from any object types.