Prepend/Append Data with RxJS Operators concat and startWith

Share this video with your friends

Send Tweet

Some Observables may complete, and we may want to append another Observable to the one which just completed. This lesson teaches you how to use the concat() operator for either appending or prepending, and how the shortcut operator startWith() is an easy way of prepending values to an Observable.

🚨 Since we are importing interval from RxJS, we don't need to preface our Observables with Rx.Observable. You can no longer .{operator}, you need to .pipe({operator}) instead.

Jon
Jon
~ 8 years ago

var bar = foo.startWith('a'); gives me a TypeScript compile error: Argument of type 'string' is not assignable to parameter of type 'number | Scheduler'.

However, changing it to var bar = foo.startWith(1); gives me no such error. Thoughts?

André Staltz
André Staltz(instructor)
~ 8 years ago

Hi Jon. With TypeScript, you would need to declare foo as Observable<string | number>, because otherwise it will infer that foo has type Observable<number> and we can't pretend a string is a number. Observable<string | number> means "this observable may emit either strings or numbers".

ganqqwerty
ganqqwerty
~ 7 years ago

amazing, I finally understand startWith!!!