Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to take an iterable object as a function parameter? (Typescript)

example:

function foo(iterable) {
    for (let i of iterable) {}
}

What type could iterable be here asides from any? other examples include Array.from and most of the other iterable data structure constructors.

like image 568
jpanda109 Avatar asked Apr 15 '26 01:04

jpanda109


1 Answers

Iterable is an ES6 feature, so setting your tsc target to "es6" lets you take in an Iterable as a parameter, e.g.

function foo<T>(iterable: Iterable<T>) {
    for (let i of iterable) {}
}
like image 104
jpanda109 Avatar answered Apr 17 '26 15:04

jpanda109