Search code examples
angulartypescriptrxjstypescript-genericsnest

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.


Solution

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

    This is why your compiler is complaining.

    Your AwaitedObservable type is fine.