One of my challenges involves a model class that represents the server response:
class ServerResponse {
code: number;
response: string;
}
Whenever I make api calls, I want the response to always be of type Observable<ServerResponse>
, even in case of errors:
callApi() : Observable<ServerResponse> {
return this.http.post(this.endpoint, '')
.pipe(
// ....
catchError(err => {
// ...
return of(new ServerResponse());
}
)
}
However, I encountered a TypeScript error:
Type 'Observable<Object | ServerResponse>' is not assignable to type Observable<ServerResponse>
I am wondering why the of
method is returning
Observable<Object | ServerResponse>
. Any insights on this issue would be greatly appreciated.
Thank you.