While running my program, I encountered the error 'Type 'undefined' is not assignable to type 'CartItem'. Unfortunately, I am unable to resolve this issue :(.
import { Injectable } from '@angular/core';
import { CartItem } from '../common/cart-item';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class CartService {
cartItems: CartItem[] = [];
totalPrice: Subject<number> = new Subject<number>();
totalQuantity: Subject<number> = new Subject<number>();
constructor() {}
addToCart(theCartItem: CartItem) {
// check if we already have the item in our cart
let alreadyExistsInCart: boolean = false;
let existingCartItem: CartItem = undefined; // ERROR HERE
}
}
The error message Type 'undefined' is not assignable to type 'CartItem' is troubling me. How should I go about resolving this issue?