Currently, I am working through the Angular tutorial that can be found at https://angular.io/start. After successfully completing the tutorial, I decided to practice building for production locally. However, when attempting to build, I encountered this error:
error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. this.items.push(product);
The object 'product' that is being added to the items array is defined as follows in the tutorial (in a separate file named products.ts):
export const products = [
{
id: 1,
name: 'Phone XL',
price: 799,
description: 'A large phone with one of the best screens'
},
{
id: 2,
name: 'Phone Mini',
price: 699,
description: 'A great phone with one of the best cameras'
},
{
id: 3,
name: 'Phone Standard',
price: 299,
description: ''
}
];
Below is part of the code where I am trying to use it:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { products } from './products';
@Injectable()
export class CartService {
items = [];
constructor(
private http: HttpClient
) {}
addToCart(product) {
this.items.push(product);
}
getItems() {
return this.items;
}
clearCart() {
this.items = [];
return this.items;
}
getShippingPrices() {
return this.http.get('/assets/shipping.json');
}
}
I have attempted to define the items array in multiple ways, all resulting in errors:
items = []
items : any[] = [];
items : {id: bigint, name: string, price: bigint, description, string}[] = [];
If you have any suggestions or solutions, your input would be greatly appreciated.