【问题标题】:combineLatest correct syntax结合最新的正确语法
【发布时间】:2021-08-29 21:52:16
【问题描述】:

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

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

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

在我的情况下,Rboolean

这是我尝试使用该函数的代码 sn-p,但 Visual Studio Code 显示带有删除线样式的调用,并且表明它已被弃用。

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 说:

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 文档中记录的签名。

你能帮我理解为什么吗?

【问题讨论】:

  • 您使用的是 rxjs v6 吗?看起来 resultSelector 将被弃用 (combineLatest@6.67),但在 v7 中似乎并非如此:combineLatest@latest
  • @BizzyBob 是的,我使用的是 v6,但即使更新到 v7 也没有使弃用警告消失。

标签: syntax rxjs deprecated deprecation-warning combinelatest


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我建议让运算符(如函数)做一件事来保持简单。无论 combineLatest() 中可能/可能不会弃用哪些功能,您都可以使用它来组合 observables。之后,您可以使用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)
      )
    );
    

    【讨论】:

    • 虽然这是一个很好的建议,我肯定会遵循它(顺便说一句,谢谢),它并不能真正回答我的(更新的)问题,但它只能解决问题,所以我觉得我不应该接受它。
    • 在您的原始代码中,尝试将您的 observables 作为单个数组而不是多个值传递。 combineLatest([obs1$, obs2$, obs3$...] 这应该会改变类型推断,而不是给你这个有问题的错误。如果您使用的是版本 7,这应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多