Reputation: 71
I try to understand Observable in Angular 4. I am watching a video about it and I want to create my first Observable, but get an error in my IDE:
Generic type Observer requires 1 types argument(s)
My code:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { Observer } from 'rxjs/Observer';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor() { }
ngOnInit() {
const myObservable = Observable.create((observer: Observer) => {
setTimeout(() => {
observer.next('first package');
}, 2000);
});
}
}
I guess that I should add generic type like this: Observable<any>
, but in the video which I watched the author don't add any generics and it works.
Can anyone explain me why?
Upvotes: 1
Views: 4761
Reputation: 61
Your error is telling you exactly what to do. Generic type Observer requires 1 types argument(s). You just need to pass the data type that are using for your Observer. In this case it's a string 'first package'. So, in your .create()
method set the observer data type to a string like this:
observer: Observer<string>
Upvotes: 2