I am currently facing an issue with the TS interface shown below:
export interface Item {
product: string | Product;
}
When I try to iterate through an array of items, I need to handle the type checking. For example:
items = Items[];
items.forEach(item => {
item.product._id
})
This code will not work as the property _id is not applicable to strings. Therefore, I have to check the type beforehand like this:
items = Items[];
items.forEach(item => {
if (typeof item.product === 'object') item.product._id
})
The solution works but the code doesn't look very clean. How would you approach this situation?