Reputation: 32766
With this code
const start$ = Observable.fromEvent(this.getNativeElement(this.start),'click');
const stop$ = Observable.fromEvent(this.getNativeElement(this.stop),'click');
const interval$ = Observable.interval(1000);
const data = {count:0};
const intervalThatStops$ = interval$
.takeUntil(stop$);
start$
.switchMapTo(intervalThatStops$)
.startWith(data)
.scan((acc,curr) => {
return {
count: acc.count+1
};
})
.subscribe((x)=> console.log(x.count));
I've got in the console
Argument of type '{ count: number; }' is not assignable to parameter of type 'number | IScheduler'. Type '{ count: number; }' is not assignable to type 'IScheduler'. Property 'now' is missing in type '{ count: number; }'.)
a quick fix should be
.startWith<any>(data)
but what's the right way, please ?
Thanks in advance
Upvotes: 0
Views: 442
Reputation: 14574
Taking it in parts,
start$
.switchMapTo(intervalThatStops$)
.startWith(data)
After that switchMapTo, your resulting Observable will emit values from the intervalThatStops$. And intervalThatStops$ is emitting pure numbers. So, when you try to .startWith({count:0})
, you get the Typescript error because your observable is emitting numbers, but you're trying to startWith an object ({count: 0}).
The solution is to map your intervalThatStops$ to your object BEFORE using the startWith.
You can do this numerous ways - one possibility is:
start$
.switchMapTo(intervalThatStops$)
.map(i => ({count: i})))
.startWith(data)
Upvotes: 2