I'm trying to use combineLatest
, in particular I need to combine four Observables that emit values of different types with a projection function that returns a boolean type.
From what I get from the Rxjs documentation here, I think that this is the combineLatest
signature I need to use, which is not deprecated:
combineLatest(sources: readonly any[], resultSelector: (...values: A) => R): Observable<R>
with R
being boolean
in my case.
Here is my code snippet where I try to use that function, but Visual Studio Code shows the call with strikeout style and it suggests it's deprecated.
this.canBook$ = combineLatest(
this.userSvc.canRedirect(this.fbSvc.getUserId()),
this.userSvc.canBook(this.fbSvc.getUserId()),
this.calSvc.segnaleOrarioIso8601(),
this.selectedDay$,
(canredir: boolean, canbook: boolean, iso8601: string, selday: ICGiorno) => {
return canredir && (dayjs(selday.iso8601).diff(iso8601, 'hour') > 0) ||
canbook && (dayjs(selday.iso8601).diff(iso8601, 'hour') >= 24);
} );
VS Code says:
The signature '(v1: Observable<boolean>, v2: Observable<boolean>, v3: Observable<string>, v4: Subject<ICGiorno>, resultSelector: (v1: boolean, v2: boolean, v3: string, v4: ICGiorno) => boolean, scheduler?: SchedulerLike): Observable<...>' of 'combineLatest' is deprecated.ts(6387)
However I'm not passing in any scheduler parameter, so I don't understand why VS Code is matching my call with the deprecated signature instead of the one documented above in the Rxjs API doc.
Can you please help me understand why?
As mentioned in the comments, the overload you're using is deprecated in [email protected] combineLatest, and there is no overload without scheduler
, but instead, there are some overloads with scheduler
, which are deprecated, e.g:
/** @deprecated resultSelector no longer supported, pipe to map instead */
export function combineLatest<O1 extends ObservableInput<any>, R>(sources: [O1], resultSelector: (v1: ObservedValueOf<O1>) => R, scheduler?: SchedulerLike): Observable<R>;
/** @deprecated resultSelector no longer supported, pipe to map instead */
export function combineLatest<O extends ObservableInput<any>, R>(sources: O[], resultSelector: (...args: ObservedValueOf<O>[]) => R, scheduler?: SchedulerLike): Observable<R>;
But in RxJS@latest combineLatest you can find the overload you mentioned in your question, and it's not deprecated, but the problem in your code is that you're using another overload than mentioned one, which is deprecated also:
/** @deprecated Pass an array of sources instead. The rest-parameters signature will be removed in v8. Details: https://rxjs.dev/deprecations/array-argument */
export function combineLatest<A extends readonly unknown[], R>(
...sourcesAndResultSelector: [...ObservableInputTuple<A>, (...values: A) => R]
): Observable<R>;
So, to fix it you just need to use the overload you mentioned correctly (which is not deprecated in RxJS@latest
) like the following:
this.canBook$ = combineLatest(
[ // <<<< The mentioned overload expects an array not Observables as params.
this.userSvc.canRedirect(this.fbSvc.getUserId()),
this.userSvc.canBook(this.fbSvc.getUserId()),
this.calSvc.segnaleOrarioIso8601(),
this.selectedDay$,
], // <<<<
(
canredir: boolean,
canbook: boolean,
iso8601: string,
selday: ICGiorno
) => {
return (
(canredir && dayjs(selday.iso8601).diff(iso8601, 'hour') > 0) ||
(canbook && dayjs(selday.iso8601).diff(iso8601, 'hour') >= 24)
);
}
);