My situation involves handling data from a 3rd party API that consists of multiple properties, all stored as strings. Unfortunately, even numbers and booleans are represented as strings ("5" and "false" respectively), which is not ideal. I am determined to rectify this issue.
To address this, I have created a type to receive the API response and store the corrected values:
interface Person {
age: string | number,
name: string,
hasChildren: string | boolean,
...
}
My goal is to convert data like this:
const responseFromApi: Person = {
age: "20",
name: "John",
numberOfChildren: "true"
...
}
into this format:
const afterTreatment: Person = {
age: 21,
name: "John",
numberOfChildren: true
...
}
It's worth noting that my actual object contains many more properties than shown in this example. Therefore, individually addressing each property is not a feasible solution for me.
My plan is to iterate through the object and convert any eligible values to their correct types based on the defined interface.