结合最新的正确语法

combineLatest correct syntax

我正在尝试使用 combineLatest,特别是我需要将发出不同类型值的四个 Observable 与 returns 布尔类型的投影函数结合起来。

根据我从 Rxjs documentation here 得到的信息,我认为这是我需要使用的 combineLatest 签名,它没有被弃用:

combineLatest(sources: readonly any[], resultSelector: (...values: A) => R): Observable<R>

在我的例子中 Rboolean

这是我尝试使用该函数的代码片段,但是 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 Code 将我的调用与已弃用的签名相匹配,而不是上面 Rxjs API 文档中记录的签名。

你能帮我理解为什么吗?

我建议让运算符(如函数)只做一件事,从而使它们保持简单。无论 may/or 中哪些功能可能不会在 combineLatest() 中弃用,您都可以使用它来组合可观察值。之后,您可以使用 map() 运算符来 return 您的布尔值。

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)
  )
);

如评论中所述,您正在使用的重载在 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)
    );
  }
);