TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. It invokes the iterable protocol by calling the object’s [Symbol.iterator]() method, retrieving the values yielded by the iterator and abstracting away the manual management of loop counters or .next() state evaluations.
Syntax
variable: Receives the value yielded by the iterator on each step. It is typically declared withconstorlet. TypeScript automatically infers the type of this variable based on the generic type of the iterable.iterable: Any object that structurally satisfies the iterable protocol, meaning it possesses a property with aSymbol.iteratorkey that returns an iterator.
Type Inference
TypeScript leverages the type signature of the iterable to enforce type safety within the loop block. If an array is typed asnumber[], the loop variable is strictly inferred as number.
Implementing the Iterable Protocol
To utilize afor...of loop on a custom TypeScript class or object, the entity must structurally satisfy the iterable protocol. Because TypeScript uses structural typing, an explicit implements Iterable<T> declaration is entirely optional. The object only needs to define a valid [Symbol.iterator]() method that returns an object conforming to the Iterator<T> interface, which in turn yields IteratorResult<T> objects.
Asynchronous Iteration
TypeScript supports thefor await...of variant to consume objects implementing the AsyncIterable<T> protocol. This is used when the iterator’s next() method returns a Promise<IteratorResult<T>>.
Compiler Configuration (downlevelIteration)
When the TypeScript compiler target (compilerOptions.target) is set to ES5 or ES3, native support for the iterable protocol does not exist. By default, TypeScript will only allow for...of loops on standard Arrays and string types in these environments.
To iterate over other iterables (like Map, Set, or custom iterable implementations) when targeting ES5/ES3, the downlevelIteration flag must be enabled in tsconfig.json.
__values) into the emitted JavaScript to manually emulate the ES6 iteration protocol, ensuring runtime compatibility at the cost of slightly increased bundle size.
Master TypeScript with Deep Grasping Methodology!Learn More





