我正在尝试使用combineLatest,特别是我需要将四个发出不同类型的值的可观察对象与一个返回布尔类型的投影函数组合在一起。
从我从Rxjs documentation here得到的信息来看,我认为这是我需要使用的combineLatest签名,它没有被弃用:
combineLatest(sources: readonly any[], resultSelector: (...values: A) => R): Observable<R>在我的情况下R是boolean。
下面是我尝试使用该函数的代码片段,但Visual Studio代码显示了带有删除线的调用,并表明它已被弃用。
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代码说:
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)但是,我没有传入任何调度器参数,所以我不明白为什么VS代码将我的调用与弃用签名匹配,而不是上面Rxjs API文档中记录的签名。
你能告诉我为什么吗?
发布于 2021-08-30 09:40:33
正如评论中提到的,您正在使用的重载在RxJS@6.6.7 combineLatest中已被弃用,并且在没有scheduler的情况下不存在重载,但在scheduler中有一些重载,这些重载已被弃用,例如:
/** @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>;但是在RxJS@latest combineLatest中你可以找到你在问题中提到的重载,它并没有被弃用,但你代码中的问题是你正在使用另一个重载,而不是前面提到的重载,它也被弃用了:
/** @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>;因此,要修复它,您只需正确使用您提到的重载(在RxJS@latest中不建议使用),如下所示:
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)
);
}
);发布于 2021-08-30 05:53:16
我建议保持操作符(如函数)的简单性,让它们做一件事。无论哪些特性可能/或不会在combineLatest()中被弃用,您都可以使用它来组合可观察到的内容。之后,您可以使用map()运算符来返回布尔值。
this.canBook$ = combineLatest([
this.userSvc.canRedirect(this.fbSvc.getUserId()),
this.userSvc.canBook(this.fbSvc.getUserId()),
this.calSvc.segnaleOrarioIso8601(),
this.selectedDay$,
]).pipe(
map(
([canredir, canbook, iso8601, selday]) =>
(canredir && dayjs(selday.iso8601).diff(iso8601, "hour") > 0) ||
(canbook && dayjs(selday.iso8601).diff(iso8601, "hour") >= 24)
)
);https://stackoverflow.com/questions/68977197
复制相似问题