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

260
Visualizações
Object is of type 'unknown'

I have simple Rxjs function Observables and Operators to return new Observable.in my code i am using filter and map operators chained inside a pipe sadly, i get error TS2571: Object is of type 'unknown'. inside filter operator data The code is following :

import { filter, map, Observable } from 'rxjs'

@Component({
  selector: 'app-observable-operator',
  templateUrl: './observable-operator.component.html',
  styleUrls: ['./observable-operator.component.css']
})
export class ObservableOperatorComponent{

  title:string='Angular Observable using Observable Operators';

  ob=new Observable((observer:any)=>{
    console.log("Observable Starts");
    observer.next(1)
    observer.next(2)
    observer.next(3)
    observer.next(4)
    observer.next(5)
    observer.complete()
  }).pipe(filter(data=>
    data > 2), //Error inside filter operator (parameter) data:unknown
    map((val)=>{return val as number*2}),

  )

}


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

0

I believe you need to add typing to the param

filter((data: any) => data > 2)
over 4 years ago · Santiago Trujillo Relatório

0

The way to fix this issue is to define the type of the observer right.

The observer parameter in the function passed to the constructor of the Observable is of type Observer<number>, since it notifies numbers. If you do this, Typescript can infer the type of data as number.

In other words the code should be this

ob=new Observable((observer: Observer<number>)=>{
  console.log("Observable Starts");
  observer.next(1)
  observer.next(2)
  observer.next(3)
  observer.next(4)
  observer.next(5)
  observer.complete()
}).pipe(filter(data=>
  data > 2), //Error inside filter operator (parameter) data:unknown
  map((val)=>{return val as number*2}),

)
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