Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Interface

IterableIterator

IterableIterator represents an object that is both iterable and an iterator. It can participate in for-of and similar iteration constructs while producing values through the iterator protocol.

How it works

IterableIterator describes an object that combines the iterable and iterator protocols. It can provide its own iterator through Symbol.iterator and advance through values with next(), allowing it to work with constructs such as for-of.

Syntax

interface IterableIterator<T> extends Iterator<T> {
  [Symbol.iterator](): IterableIterator<T>;
}

Returns

An IterableIterator is an object usable as both an iterable and an iterator; its next method produces iterator results.

Key points

  • IterableIterator combines the roles of Iterable and Iterator.
  • It represents the protocol members needed by constructs such as for-of.
  • Its iterable side is based on providing Symbol.iterator.
  • Calling Symbol.iterator supplies the iterator used to obtain iteration values.
  • A for-of loop starts iteration by invoking Symbol.iterator.
  • Use for-of when consuming yielded values rather than property keys.

Common mistakes

  • The type describes a protocol rather than guaranteeing every convenience method associated with iteration.
  • With an ES5 target, non-array iterator usage in for-of requires appropriate compiler support such as downlevelIteration.

Example: A generator as an IterableIterator

function* values(): IterableIterator<number> {
  yield 1;
  yield 2;
}

for (const value of values()) {
  console.log(value);
}

The generator returns an object that can both produce iterator results and be consumed directly by a for-of loop.

When to use it

Typed generators

A generator function yields a sequence that callers consume with for-of.

Lazy traversal

An API exposes a lazy sequence without allocating an array of all values.

Custom iterator protocols

A custom object supports both direct iteration and explicit next() calls.

  • Iterable
  • Iterator
  • Generator
  • IteratorResult
  • Symbol.iterator

Source: Iterators and Generators. Checked 2026-10-15.