Arrays should not be treated as observables #6954
Replies: 1 comment
-
Hi @SirHall. I think this is the docs issue. Many creating/pipeable operators receive ObservableInput as a valid type that can be converted to an Observable. Thus, I have a plan to add more docs to the ObservableInput and I could use an example from this issue to distinguish between various cases, just like you mentioned: BTW, depending on what you need, you may want to replace |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, many functions will almost magically treat arrays of values as observables - and often not reliably.
For example,
of([ 1, 2, 3])
will return exactly what is passed in, whilstdefer(() => [1, 2, 3])
will seemingly without reason return the values1 -> 2 -> 3
as separate emissions - which has often been the cause of many bugs in my experience - especially if you are actually trying to insert an array into the chain without needing to restructure your code.To provide another example,
switchMap
also accepts a lambda which when returning an array - I would expect it to fail, but instead it accepts arrays as a valid observable and will happily accept it and fire the individual elements.This as a result means that if somewhere I make a mistake in how I construct or unwrap my higher-order observables, that as a result I will get very confusing error messages.
Generally I would expect TypeScript's strong typing to catch this sort of thing, but instead I get the same issue in TypeScript that I would get in JavaScript anyway in that I now must explore up the chain to figure out exactly where the type changes.
Now on this point there can be many technical arguments regarding 'elegant-ness' but ECMAScript quite famously tried to do the same thing, and as a result we have all the fun quirks of the language we're used to today.
I would argue that arrays should not automatically be treated as observables, and should instead be explicitly transformed into one.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions