I have two interfaces as below:
interface Interface1 {
a: string;
b: string;
c: {
d: string;
};
}
interface Interface2 {
aa: string;
b: {
cc: string;
};
d: string;
}
And an object:
const object1: DeepPartial<Interface1> = {
a: 'a',
c: {
d: 'd',
}
};
I'd like to create a new object object2
of type DeepPartial<Interface2>
using some kind of mapping. I can do this using a bunch of if-statements but am wondering if there might be a cleaner way? For example, I could create a mapping like:
const mapping = [
['a', 'aa'],
['c.d', 'b.cc']
['b', 'd']
]
which would recursively look through the object and produce:
const object2: DeepPartial<Interface2> = {
aa: 'a',
b: {
cc: 'd'
}
}
Notice how object2
does not include d
as b
was not present in object1
.
To summarise, I'd like to map a DeepPartial<Interface1>
to a `DeepPartial recursively, so that properties that are ommited do not get mapped. I'm also open to any suggestions about how I could approach this problem differently.
For reference, the DeepPartial
type is from here