observable icon indicating copy to clipboard operation
observable copied to clipboard

Observable.from should throw primitive iterables (strings)

Open benlesh opened this issue 11 months ago • 3 comments

Along with how Iterator helpers behave, if a user passes a string, even though it's an iterable, it's likely an error. We should throw a TypeError in that case.

Observable.from('some string'); // TypeError

benlesh avatar Mar 05 '24 22:03 benlesh

I think you misread my comment (which was not all that clear, sorry). Iterator.from does not throw when given a string. This makes it different from flatMap, which does throw when the mapper returns a string, even though strings are iterable.

See discussion in https://github.com/tc39/proposal-iterator-helpers/issues/244.

bakkot avatar Mar 05 '24 22:03 bakkot

FYI: ReadableStream.from rejects string https://github.com/whatwg/webidl/pull/1397

petamoriken avatar Aug 17 '24 11:08 petamoriken

If someone is intentionally passing a string into an X.from method, they explicitly want to convert a string into an X. I think this is the one case where an iterable string must be accepted.

ljharb avatar Aug 17 '24 21:08 ljharb