When data is received from the server in JSON format, it typically looks like the example below (details have been modified):
{
"apple": {
"fruitName": "apple",
"types": {
"greenApple": {
"productCode": "P1",
"productName": "Green Apple",
"color": "green",
},
"galaApple": {
"productCode": "P2",
"productName": "Gala Apple",
"color": "light red",
},
"redDelicious": {
"productCode": "P3",
"productName": "Red delicious apple",
"color": "bright red",
}
}
},
"orange": {
"fruitName": "orange",
"types": {
"mandarin": {
"productCode": "P5",
"productName": "Mandarin Oranges",
"color": "orange",
},
"bloodOrange": {
"productCode": "P6",
"productName": "Blood Orange",
"color": "red",
}
}
},
}
I am currently designing an Interface structure using Typescript for a project.
My progress so far:
export type FruitTypes = {
[key: string]: Product,
}
export type Product = {
productCode: string
productName: string
color: string
}
export type Fruits = {
fruitName: string
type: object<FruitTypes>
}
The challenge I am facing is how to properly declare the 'type' property in the 'Fruits' interface.
type: object<FruitTypes>
does not work as expected since it needs to be an object containing objects. How can this be accurately represented in Typescript?