У меня есть пара функций, в которых одинаковые ошибки:
TS2345: Argument of type 'Products | undefined' is not assignable to parameter of type 'Products'. Type 'undefined' is not assignable to type 'Products'.
export type Products = {
id: number,
title: string,
images: string[],
currentImageIndex: number,
isCycleMode: boolean,
cantGoPrev: boolean,
cantGoNext: boolean,
price: number,
company: string,
info: string,
inCart: boolean,
count: number,
total: number
}
getItem = (id: number) => {
return this.state.products.find(item => item.id === id);
};
addToCart = (id: number) => {
let tempProduct: Products[] = [...this.state.products];
//тут ошибка(this.getItem(id))
const index = tempProduct.indexOf(this.getItem(id));
const product = tempProduct[index];
product.inCart = true;
product.count = 1;
const price = product.price;
product.total = price;
this.setState(
() => {
return {
products: tempProduct,
cart: [...this.state.cart, product]
}
}
,
() => {
this.addTotals();
}
);
};
increment = (id: number) => {
let tempCart = [...this.state.cart];
const selectedProduct = tempCart.find(item => item.id === id);
// тут такая же ошибка indexOf(selectedProduct)
const index = tempCart.indexOf(selectedProduct);
const product = tempCart[index];
product.count = product.count + 1;
product.total = product.count * product.price;
this.setState(
() => {
return {
cart: [...tempCart]
};
},
() => {
this.addTotals();
}
);
};