I am currently working in angular, my http client returns Observable<HttpResponse<T>>. I am trying to create a util function that wraps this and returns a Promise<T>
const response = await baseResponseHandler(this.api.getAccountInfo('response'); //response is unknown
here is the util function:
export async function baseResponseHandler<
K,
T extends Observable<HttpResponse<K>>,
>(arg: T): Promise<K> {
const value = await firstValueFrom(arg); //rxjs Observable -> Promise
if (value.ok) {
return value.body;
}
return value.body;
}
Why is it not infering the type correctly?
>Solution :
export async function baseResponseHandler<K>(
arg: Observable<HttpResponse<K>>
): Promise<K> {
const value = await firstValueFrom(arg);
if (value.ok) {
return value.body;
}
return value.body;
}
Now when you call it
const response = await baseResponseHandler<myType>(this.api.getAccountInfo('response');
I hope this helps you. Cheers 🥂