It might be a good idea to update the call signature for example()
using a mapped type applied to an array:
declare function example<T extends any[]>
(...obs: { [K in keyof T]: Observable<T[K]> }): T;
In this case, the type T
corresponds to the output array/tuple type, and the input parameter obs
maps T
so that each element is encapsulated within Observable<>
. Thanks to inference from mapped types, T
can automatically be deduced from the type of obs
when you invoke example()
:
const result = example(of(''), of(0));
// const result: [string, number]
This should give you the desired type.
Keep in mind that although the question doesn't specifically address the implementation details, there may be potential issues down the line. While I'm no rxjs expert, it appears that converting an Observable<X>
to an X
synchronously could pose challenges because the observable might not have a value of type
X</code available immediately upon calling <code>example()
. One approach could be to have
example()
throw an error if the inputs lack ready values. Alternatively, it might make more sense for
example()
to transform
[Observable<string>, Observable<number>]
into
Observable<[string, number]>
, where the output observable waits until each input has a value before emitting. Just something to keep in mind.
Code playground link