I get product data via API, the data is deeply nested, properties might change according to the product definition. Do I need to fully type the product data in order to use typescript correctly? Or how would you approach deeply nested data in the frontend?
const product = {
id: 1,
colors: ["red", "green"],
comments: null,
variants: [
{
id: 1,
name: "t-shirt",
comments: null,
material: [
{
id: 1,
name: "cotton",
comments: ["Hello World"],
}
]
}
]
}
function getProduct<T>(product: T) {
return product
}
Property 'variants' does not exist on type 'T'.
function setProductFirstVariantComment<T>(product: T, comment: string) {
product.variants[0].comments = [comment] // TS ERROR
return product
}
productand in the same time mutate it. As per my understanding, if you want to retyrn new product, you should return new reference or, if you mutate product, you should not return anything