Iterable
Syntax
interface Iterable<T> {
[Symbol.iterator](): IterableIterator<T>;
}
An interface representing an object that can be iterated over.
Example
import { Iterable } from 'potence';
function sum(iterable: Iterable<number>): number {
let sum: number = 0;
for (const item of iterable) {
sum += item;
}
return sum;
}
sum([0, 1, 2]); // -> no error
sum(new Set([0, 1, 2])); // -> no error
Remarks
Any objects that implement Iterable
can be iterated over using a for ... of
loop.
All base collection types (
arrays,
Sets,
Maps,
strings,
and
TypedArrays
) implement Iterable
and are assignable to it.
This type also has a type guard.