Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
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 answers
Answer question

0

I believe you need to add typing to the param

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!