I have a JSON object which contains a bunch of strings...
{
id: "1",
amount: "57,000",
title: "I am a test"
}
How can I convert this into an interface such as:
interface MyObj {
id: number;
amount: number;
title: string;
}
Would I need to map over each object property and try to convert to the necessary type or can I simply try and force cast a JSON object into a type?
For clarity, here is some additional information.
I have an array of the example objects above, the JSON so...
[
{
id: "1",
amount: "57,000",
title: "I am a test"
},
{
id: "2",
amount: "2347,000",
title: "I am a test as well"
}
]
All the values within this JSON object are string. Each time we use the objects in TS, we want their types to be correct. For example, the id and amount should be numbers, they can be converted before they are stored in our state.
So I would like to convert the JSON objects in the array above into a specific interface or type. Because the types do not match I cannot just use the interface, I need to do some conversions or try and cast to a type before actually aligning to the interface. This is the question, how can I map over the JSON objects in the array and convert some of the keys from string to number (possibly?).