I need to create a function called addId
that takes an object as input and returns the same object with an added property _id: string
in every sub-object.
Let's consider the input object constructed from the following class.
class A {
a: number
b: { bb: number }
c: number[]
constructor() {
this.a = 1
this.b = { bb: 2 }
this.c = [1,2,3]
}
}
const a = new A()
If we run addId(a)
on this object, the function would return
A {
_id: 'id1'
a: 1
b: { bb: 2, _id: 'id2' }
c: [1,2,3]
}
The parameter for addId
can be an object of any class, not necessarily the class A
. It can be constructed recursively from json, objects, and arrays with any nesting depth.
I am not looking for the implementation of addId
, just the expected return type. I believe the solutions will involve similar techniques to those used for the type DeepReadonly<T>
defined in this GitHub repository.