how to solve Type 'any[]' is not assignable to type 'never[]'. Type 'any' is not assignable to type 'never'

Issue

so iam tying to show api using ionic but this error show

Type ‘any[]’ is not assignable to type ‘never[]’.
Type ‘any’ is not assignable to type ‘never’.

import { Component, OnInit } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { MovieService } from 'src/app/services/movie.service';

@Component({
  selector: 'app-movies',
  templateUrl: './movies.page.html',
  styleUrls: ['./movies.page.scss'],
})
export class MoviesPage implements OnInit {
  movies = [];
  currentPage = 1;
  constructor(private movieService: MovieService, private loadingCtrl: LoadingController) { }

  ngOnInit() {
    this.loadMovies();
  }
  async loadMovies() {
    const loading = await this.loadingCtrl.create({
      message: 'Loading..',
      spinner: 'circles',
    });
    await loading.present();
    this.movieService.getTopRatedMovies(this.currentPage).subscribe((res) => {
      loading.dismiss();
      this.movies = [...this.movies, ...res.results];
      console.log(res);
    });
  }
}

Solution

TS doesn’t infer a type from the empty array.

You’ll need to defined that type : movies: Movie[] = [];

Answered By – Matthieu Riegler

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published