I've been diving into Angular lately and I'm facing some challenges with handling get requests. If you're interested, you can check out my code on Angular Stackblitz
import { HttpClient} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, map, Observable, throwError } from 'rxjs';
@Injectable({
providedIn:'root'
})
export class PostsService {
constructor(private http:HttpClient) { }
getPosts():Observable<Post[]>{
return this.http.get<any>('https://dummyjson.com/posts').pipe(
map(res=> res.posts),
catchError(err=>{
console.log(err)
return throwError(()=>err)
})
)
}
}
interface PostResponse{
limit: number
posts: string[]
skip: number
total: number
}
interface Post {
body: string
id: number
reactions: number
tags: string[]
title: string
userId: number
}
Typically, I resort to using the any
type instead of PostResponse
or Post[]
, but I realize it might not be the best practice. The map
operator adds complexity to the situation.
So, in this scenario, what would be the ideal type for the get
request?