0
addToCart (event: any) {
    if ("cart" in localStorage) {
      this.cartProducts = JSON.parse(localStorage.getItem("cart")!)
      console.log(event);
      let exist = this.cartProducts.find(item => item.item.id == event.item.id);
      if(exist) {
        alert("This product is already in your cart");
      } else {
        this.cartProducts.push(event);
        localStorage.setItem("cart" , JSON.stringify(this.cartProducts));
      }
    }else {
      this.cartProducts.push(event);
      localStorage.setItem("cart" , JSON.stringify(this.cartProducts));
    }    
  }

your text

I tried to solve it with type annotation but I couldn't. And there is no any quick fix to fix this problem.

  • try *item?.item?.id == event?.item?.id* – Luca Angrisani Nov 03 '22 at 13:56
  • 1
    Show how you declare `cartProducts`. – kelsny Nov 03 '22 at 14:00
  • Fairly certain this'll be a duplicate of https://stackoverflow.com/questions/52423842/what-is-not-assignable-to-parameter-of-type-never-error-in-typescript – Andrew Allen Nov 03 '22 at 14:18
  • Sorry for being picky, but if you would use typescript properly since it's already enforced by the framework you are using and you would never face that issue. Just define types/interfaces for the variables that you are operating with and you will love how intellisence will guide you forward – JSEvgeny Nov 03 '22 at 14:21

1 Answers1

0

I'm assuming you're not typing cartProducts and this starts out as an empty array.

cartProducts = []

This defaults as type never[]. See What is "not assignable to parameter of type never" error in TypeScript?

You should type this

cartProducts: ICartProduct[] = []

any is a type to avoid. The purpose of typescript is to catch errors and any will never do that for you.

Andrew Allen
  • 6,512
  • 5
  • 30
  • 73