Since updating my libraries to the latest Angular 6 and RxJS 6, I've encountered an issue.
I have a RouteService
class that functions as a service. It utilizes the HttpClient
to fetch data from a remote API. However, after the update, I'm facing a strange error during project compilation.
This is my service class:
import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs/Rx";
import {catchError} from 'rxjs/operators';
export interface Route {
name:string;
route_id:number;
created_at:Date;
}
@Injectable()
export class RouteService {
constructor(private http:HttpClient) {}
getRoutesList():Observable<Route[]> {
return this.http.get<Route[]>(`http://localhost:8090/api/routes`)
.pipe(catchError(ServiceUtil.handleError));
}
}
Here’s the handleError method:
import {HttpErrorResponse} from '@angular/common/http';
import {ErrorObservable} from 'rxjs/observable/ErrorObservable';
export module ServiceUtil {
export function handleError(error:HttpErrorResponse) {
if (error.error instanceof ErrorEvent)
console.error('An error occurred:', error.error.message);
else
console.error('An error occurred:', JSON.stringify(error.error));
return new ErrorObservable(error.error);
}
}
After running ng serve
, I encounter the following error:
ERROR in src/app/service/route-service/route.service.ts(21,5): error TS2322: Type 'Observable<{} | Route[]>' is not assignable to type 'Observable<Route[]>'.
Type '{} | Route[]' is not assignable to type 'Route[]'.
Type '{}' is not assignable to type 'Route[]'.
Property 'includes' is missing in type '{}'.
Failed to compile.
What mistake am I making? Is there an issue in my code that is causing errors in the new versions but worked fine in the old ones?