Rxjs: Rx.Observable.merge should return same type regardless of input

Created on 12 Jan 2017  路  5Comments  路  Source: ReactiveX/rxjs

https://github.com/btroncone/learn-rxjs/issues/26#issuecomment-272173571

Mr. btroncone and I are both curious and concerned about the API for Rx.Observable.merge -

it seems to return different types based off the input, which is at the very least confusing.

You can see the difference when comparing Rx.Observable.of with Rx.Observable.merge, see code below.

RxJS version:

5.0.2

Code to reproduce:

const Rx = require('rxjs');

var example = Rx.Observable.of(
    Rx.Observable.timer(1),
    Rx.Observable.timer(1),
    Rx.Observable.timer(1)
);

console.log(example.constructor.name);

var example = Rx.Observable.of([
        Rx.Observable.timer(1),
        Rx.Observable.timer(1),
        Rx.Observable.timer(1)
    ]);

console.log(example.constructor.name);


var example = Rx.Observable.merge(
    Rx.Observable.timer(1),
    Rx.Observable.timer(1),
    Rx.Observable.timer(1)
);

console.log(example.constructor.name);

var example = Rx.Observable.merge([
    Rx.Observable.timer(1),
    Rx.Observable.timer(1),
    Rx.Observable.timer(1)
]);

console.log(example.constructor.name);

The output is:

ScalarObservable
Observable
Array

Expected behavior:

ScalarObservable
Observable
SomeObservable  // <<<

Actual behavior:

ScalarObservable
Observable
Array  // <<< an array?

Additional information:

Maybe there is a good reason for this, but it seem bad on the face of things

Most helpful comment

ok so here is the thing:

if it doesn't currently accept an array (which seems clear) - it should accept an array, because it obviously should given the arguments it accepts; but if the API really really can't accept an array, then it should throw an error IMO.

if it does accept an array, then please make the return value consistent, in this case, an Observable

I like TypeScript and am using it to interface with RxJS in some case, but in other cases I am interfacing with RxJS with plain JS and nothing is really stopping me from passing an array to the merge method etc.

It would help if:

(a) it allowed for arrays, (which it really should)
or
(b) throw an error if an array is passed

my 2 cents

All 5 comments

merge does not accept an Array. See the declarations.

If you pass it a single array argument you would hit this conditional, which would just return the Array back casted to an Observable.

If you want to use it with an array you need to destructure it, the following should work:

var example = Rx.Observable.merge(...[
    Rx.Observable.timer(1),
    Rx.Observable.timer(1),
    Rx.Observable.timer(1)
]);

ok so here is the thing:

if it doesn't currently accept an array (which seems clear) - it should accept an array, because it obviously should given the arguments it accepts; but if the API really really can't accept an array, then it should throw an error IMO.

if it does accept an array, then please make the return value consistent, in this case, an Observable

I like TypeScript and am using it to interface with RxJS in some case, but in other cases I am interfacing with RxJS with plain JS and nothing is really stopping me from passing an array to the merge method etc.

It would help if:

(a) it allowed for arrays, (which it really should)
or
(b) throw an error if an array is passed

my 2 cents

I am considering this issue as same as #2294 and #2144, about runtime validation and soundness, linking each.

I'll use #2153 as umbrella issue to discuss runtime validations.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings