Home > Software design >  Type '' is not assinable to type never[]
Type '' is not assinable to type never[]

Time:11-28

loadCartItems() {
    this.cartService.getCartItems().subscribe(
        (items: CartItem[]) => { this.cartItems = items; }
    )
}

The code above gives me the error message Type 'CartItem[]' is not assignable to type 'never[]'. Type 'CartItem' is not assignable to type 'never'.

I have tried to find a solution but have been unsuccessful. What is causing this error?

here's my cart.service

@Injectable({
  providedIn: 'root'
})
export class CartService {

  constructor(private http: HttpClient) { }

  getCartItems(): Observable<CartItem[]> {

    return this.http.get<CartItem[]>(cartUrl).pipe(
      map((result: any[]) => {
        let cartItems: CartItem[] = [];

CodePudding user response:

I think you should change code like this.

return this.http.get<CartItem[]>(cartUrl)

Or, if you edit result from API you should change like this.

    return this.http.get<CartItem[]>(cartUrl).pipe(
      map((result: CartItem[]) => {
        const editResult = result
        // edit editResult
        return editResult 
      })
  • Related