When the following code snippet is executed:
@Mutation
remove_bought_products(productsToBeRemoved: Array<I.Product>) {
const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
const reducedProductsInVendingMachine: Array<I.Product> =
tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
...
An error message is produced:
TS2740: Type '{}' is missing the following properties from type 'Product[]': length, pop,
push, concat, and 28 more.
250 |
251 | const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
> 252 | const reducedProductsInVendingMachine: Array<I.Product> =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
253 | tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
254 | productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
The question arises: What is the return type of the reducer function in this context?
The products being manipulated are structured as follows:
[{
id: 1,
productName: "coke",
productPrice: 2,
productQty: 20
},
...
]