I have a few different scenarios on my mind.
Imagine if I make an http call to fetch all movies from my php backend api. This is observable, so I need to subscribe to it.
// Here's my service getAll() : Observable<Movie[]>{ this.http.get("/movies"); } // And in my component, I subscribe to this getMovies(){ this.movieService.getAll().subscribe(movies=>{ this.movies = movies; }) }
The main question here is why do I need to specify Observable<Movie[]>
as the return type for the getAll()
function in my service instead of just using getAll()
without specifying a return type or specifying Observable<any>
. The advantage of explicitly stating Movie[]
is what exactly? I understand that in the getAll()
function it simply returns an observable and then casts it to Observable<Movie[]>
.