jpanda109
jpanda109

Reputation: 73

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.

Upvotes: 3

Views: 1472

Answers (2)

jpanda109
jpanda109

Reputation: 73

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) {}
}

Upvotes: 3

Sing
Sing

Reputation: 4052

You should tell typescript the type of your parameter, so it could compile:

function foo(iterable:Array<any>) {
    for (let i of iterable) {}
}

Types could iterate

Upvotes: 1

Related Questions