'Type 'Error' provides no match for the signature '(this: Observable<{}>, subscriber: Subscriber<{}>): TeardownLogic'

I had angular 5.2.11, but today I updated it to 6, so when I try to build, I got an error says :

TS2345: Argument of type Error is not assignable to parameter of type (this: Observable<{}>, subscriber: Subscriber<{}>) => TeardownLogic.   Type Error provides no match for the signature (this: Observable<{}>, subscriber: Subscriber<{}>): TeardownLogic.

serviceInvoke<R,B>(baseUrl: string, relativeUrl: string, requestMethod: RequestMethod, params: {
  [key: string]: any | any[]
}, body ? : B): Observable<R> {
  const requestOptionsArgs = {
    method: requestMethod,
    params: params,
    body: body
  };
  return this.httpClient.request(baseUrl + relativeUrl, requestOptionsArgs)
    .pipe(
      map((response: Response) => response.json()),
      catchError((httpErrorResponse: Response) => new ErrorObservable(this.getError(httpErrorResponse)))
    );
}

enter image description here



Solution 1:[1]

Pretty sure you don't really need to map and convert response to JSON explicitly by calling response.json() when using HttpClient.

That was only required when using Http.

Try getting rid of that. Something like this:

serviceInvoke<R,B>(
  baseUrl: string, 
  relativeUrl: string, 
  requestMethod: RequestMethod, 
  params: { [key: string]: any | any[] }, 
  body?: B
): Observable<R> {
  const requestOptionsArgs = {
    method: requestMethod,
    params: params,
    body: body
  };

  return this.httpClient.request(baseUrl + relativeUrl, requestOptionsArgs)
    .pipe(
      catchError((httpErrorResponse: Response) => new ErrorObservable(this.getError(httpErrorResponse)))
    );
}

Solution 2:[2]

You could face this error if you tried to create a new observable in the following way.

new Observable(new Error)

The correct way is

new Observable(observer => observer.next(new Error))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SiddAjmera
Solution 2 Tadele Ayelegn