I have a function which needs an iterable:
function fn(ary) {
for (let value of ary)
console.log(value)
}
In javascript, I would take advantage of Symbol.iterator
:
function isIterable(obj) { // checks for null and undefined if (obj == null) { return false; } return typeof obj[Symbol.iterator] === 'function'; }
Taken from Tomas Kulich original answer.
which would lead to the next code:
function fn(ary) {
if (!isIterable(ary)) throw 'Not iterable'
for (let value of ary)
console.log(value)
}
Isn't there a way to take advantage of TypeScript's typing facilities rather than using this on-the-fly function?