0
votes

I have the following code

import {HttpClient} from '@ngular/common/http';

private httpClient: HttpClient;

do_request(method: string,                                                           
        url: string,                                                              
        headers?: HttpHeaders,                                                
        content?: string,                                                         
        contentType?: string): Observable<MyResponse>{                      

   let options = {                                                                
       observe: 'response' as 'response',                                         
       responseType: 'text' as 'text',                                             
       headers: headers,
       body: content:                                                                           
   }

   return this.httpClient.request(method, url, options)                           
            .map(res=> new MyResponse(res.status, res.body))                
            .catch(err=>Observable.throw(new MyResponse(err.status)));                     
}      

This code is giving me the following error

error TS2322: Type 'Observable' is not assignable to type 'Observable'. Property 'source' is protected but type 'Observable' is not a class derived from 'Observable'

I'm using Rxjs 5.4.3, Angular 4.4.3 and TypeScript 2.5.3.

I've found several possible error sources and tried the suggested solutions with no luck:

  • Typescript version(I downgraded to 2.3 with similar results)
  • Importing a package that also has a dependency on rxjs using npm link (I installed it locally, with same result).

I will appreciate any suggestion on how to fix this issue.

1
Does it happen if you remove the catch block?Meir
@Meir The error changes: Type 'Observable<MyResponse>' is not assignable to type 'Observable<MyResponse>'. Two different types with this name exist, but they are unrelated. I removed the duplicated dependency in my code (as I explained in the questions). But maybe this is related to the fact I'm using new angular/common/http modulepablochacin

1 Answers

0
votes

Based on your comment, I think you should be explicit in your typings, hence, import Observable and declare the types:

return this.httpClient.request(method, url, options)                           
        .map((res): MyResponse => new MyResponse(res.status, res.body))                
        .catch((err):Observable<MyResponse> => Observable.throw<MyResponse>(new MyResponse(err.status)));

The important part is that the throw (as well as from and other operators) can be typed and the syntax is `Observable.operator(operands...)