Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type 'Observable<void | PostProps[]>' is not assignable to type 'Observable<PostProps[]>'

I am getting an error as :

Type 'Observable<void | PostProps[]>' is not assignable to type 'Observable<PostProps[]>'. Type 'void | PostProps[]' is not assignable to type 'PostProps[]'. Type 'void' is not assignable to type 'PostProps[]'.ts(2322)

but I am returning the response. but how map not understand?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is the error part:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, map, shareReplay } from 'rxjs/operators';

export interface PostProps {
    userId: number;
    id: number;
    title: string;
    body: string;
    processed: string;
}

@Injectable()
export class PersonnelDataService {
    list$: Observable<PostProps[]>;

    private URL = 'https://jsonplaceholder.typicode.com/posts';

    constructor(private http: HttpClient) {}

    fetchPersonnelList() {
        if (!this.list$) {
            this.list$ = this.http.get<PostProps[]>(this.URL).pipe( //error
                map((response: PostProps[]) => response),
                shareReplay(1),
                catchError(async (error) => this.handleError(error))
            );
        }
        return this.list$;
    }

    handleError(error) {
        throwError(error);
    }
}

>Solution :

getting void type because cacheError is not returning anything. So return an empty array in cacheError state

 catchError(async (error) => {
      this.handleError(error);
      return [];
  })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading