Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inferring Observable Value type using TypeScript Awaited<T> Utility Type

I was working on NestJS, at some parts of the applications. I was using the observable and wanted to get the type of value by inferring the type dynamically present in the observable similar to Awaited<T> for promise.

I tried to create a generic type that takes the observable type and resolves the value type, see below: type AwaitedObservable<T> = T extends Observable<infer R>? AwaitedObservable<R>: T;

But this does not work, the compiler yells at me and shows an error. I have no idea why is this not working, any suggestion would be helpful.

like image 959
nadendla tharak Avatar asked Aug 19 '26 23:08

nadendla tharak


1 Answers

By calling toArray you're converting your Observable<number> into Observable<number[]>.

This is why your compiler is complaining.

Your AwaitedObservable type is fine.

like image 143
Matthieu Riegler Avatar answered Aug 22 '26 14:08

Matthieu Riegler