Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
Angular, observable pipe limit?

I'm doing this in one of my route guards...

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

I'm running into an error. If there is one more switchMap added to the list, VSCode tells me... "Type 'Observable<{}>' is not assignable to type 'Observable'. Type '{}' is not assignable to type 'boolean'.ts(2322)"

Can somebody explain to me why that's the case? I can't figure it out and find related issues.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is pretty well explained in this github issue.

The gist of it is that only 9 operators are supported - any more and it will fall back to type Observable<{}> since they have to manually type everything up to 9 operators.

Use multiple pipes (maintains typesafety after the 9nth entry)

If you still want to maintain typesafety but add more than 9 operators, just call the pipe function again.

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

Manually assert the resulting type

Alternatively, you can add a manual type assertion at the end. Beware though: After the 9nth operator, the type will be infered as Observable<{}>, effectively losing typesafety.

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>;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda