LakiGeri
LakiGeri

Reputation: 2105

Observable of HttpClient can not map correctly from HttpResponse

I got a service in angular7 what gives me back some keys:

  getList(
    pageSize: number = 30,
    pageNumber: number = 0,
    filters: CKeyListFilters = <CKeyListFilters>{},
    sortByKey: string = 'activeTo',
    sortOrder: SortOrder = SortOrder.DESC
  ): Observable<ListPaginated<CKey[]>> {
    // ....

    return this.http
      .get<ListPaginated<CKey[]>>(`${environment.CApiUrl}/Get`, {
        params: params,
        observe: 'response',
      })
      .pipe(
        map((resp: HttpResponse<CKey[]>) => {
          return {
            content: resp.body,
            pagination: this.utilities.getPaginationMetaData(resp),
          } as ListPaginated<CKey[]>;
        }),
        catchError((error) => {
          throw error;
        })
      );
  }

But inside the pipe method I got this error:

error TS2345: Argument of type 'OperatorFunction<HttpResponse<CKey[]>, ListPaginated<CKey[]>>' is not assignable to parameter of type 'OperatorFunction<HttpResponse<ListPaginated<CKey[]>>, ListPaginated<CKey[]>>'.

So I want to map the HttpResponse<CKey[]> to ListPaginated<CKey[]> But I don't know how can I transform it.

I inherited this code and I'm a newbie in typescript, so any suggestion is useful for me!

Upvotes: 0

Views: 67

Answers (1)

mbojko
mbojko

Reputation: 14679

OK. Here it is:

      .get<ListPaginated<CKey[]>>(`${environment.CApiUrl}/Get`, {
        params: params,
        observe: 'response',
      })
      .pipe(
        map((resp: HttpResponse<CKey[]>) => {

in line 1, the requested resource is of type ListPaginated<CKey[]>. In line 6, the map's argument is of type HttpResponse<CKey[]>. Those types must match (or alternatively you can remove resp's typing from the map altogether).

Upvotes: 1

Related Questions