角度、可观察的管道限制?

mil*_*eow 3 visual-studio-code angular rxjs-observables

我正在我的一名路线守卫身上做这件事......

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    // go through each check sequentially, stop process if one does throwError
    return of(true).pipe( // Is there a limit to operators in a pipe???
      switchMap(() => of(true)), // simplified for this post...usually call a function that returns
      switchMap(() => of(true)),
      switchMap(() => of(true)),
      switchMap(() => of(true)),
      switchMap(() => of(true)),
      switchMap(() => of(true)),
      switchMap(() => of(true)),
      switchMap(() => of(true)),
      // if any function returns throwError, we skip other checks
      catchError(() => of(false))
    );
  }
Run Code Online (Sandbox Code Playgroud)

我遇到了错误。如果列表中再添加一个 switchMap,VSCode 会告诉我...“类型 'Observable<{}>' 不可分配给类型 'Observable'。类型 '{}' 不可分配给类型 'boolean'.ts (2322)"

有人可以向我解释为什么会这样吗?我无法弄清楚并找到相关问题。

pas*_*etz 6

这在这个 github 问题中得到了很好的解释。

其要点是仅支持 9 个运算符 - 如果再多,它将回退到键入Observable<{}>,因为他们必须手动键入最多 9 个运算符的所有内容。

使用多个管道(在第 9 个条目后保持类型安全)

如果您仍然想保持类型安全但添加超过 9 个运算符,只需再次调用管道函数即可。

source$
  .pipe(
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {})
  )
  .pipe(
    // Add the next operators after the 9nth here
    tap(() => {}) 
  )
Run Code Online (Sandbox Code Playgroud)

手动断言结果类型

或者,您可以在末尾添加手动类型断言。但要注意:在第 9 个运算符之后,类型将被推断为Observable<{}>,从而有效地失去类型安全性。

const source$:Observable<boolean> = of(true)
  .pipe(
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    tap(() => {}),
    // Typesafety is lost here
    tap(() => {}),
    tap(() => {}),

  // Manually assert the type at the end 
  // Beware that the compiler will allow pretty much anything here,
  // so "as Observable<string>" would work, even though it'd be wrong.
  ) as Observable<boolean>;
Run Code Online (Sandbox Code Playgroud)