'lastValueFrom does not exist on type 'Observable<Object>'

I have updated RxJS to version 7. In this version toPromise is deprecated and should be replaced with firstValueFrom and lastValueFrom. I however get error when I do the replacement:

import { throwError as observableThrowError, lastValueFrom } from 'rxjs';

  private get(urlPart: string, retry: number): Promise<object> {
    return this.http.get(urlPart)
      .lastValueFrom().catch(() => {
        return observableThrowError(`Could not get ${urlPart} after x attempts`);
      });
  }

Error: Property 'lastValueFrom' does not exist on type 'Observable'.

How cal I solve the issue?



Solution 1:[1]

Inspired by comments from Vovan_Super and jonrsharpe I solved the issue like this

  private get(urlPart: string, retry: number): Promise<object> {    
    let result: Promise<object> = lastValueFrom(this.http.get(urlPart)).catch(() => {
      return observableThrowError(`Could not get ${urlPart} after x attempts`);    
    });

    return result;
  }

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 Alpha Bravo Charlie ...