Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to implement Iterator<T> in Typescript, before ES6 [duplicate]

I have a project that includes many classes that ideally would implement the Iterable<T> and/or Iterator<T> interfaces. However I cannot seem to find a standard TypeScript definition of these interfaces (for example in typescript-collections or some similar package).

I understand these are somewhat standardized in ECMAScript 6 through the Symbol.iterator mechanism, but my target is ECMAScript 5 and will stay so for the foreseeable future.

Can I somehow get these interfaces without defining them myself (for future compatibility with other modules, for example)?

like image 925
Alon Avatar asked Aug 16 '16 07:08

Alon


People also ask

How do I use iterators in TypeScript?

After an iterator returns an Object with done === true and its return value, any additional calls to next() should simply return {done: true} . Notice that you can pass arguments to next() , however this is not usual. There are two other optional methods in the iterator interface, return and throw .

Which are iterator object used for iterator in es6 iterator?

Iterator is an object which allows us to access a collection of objects one at a time. An object is considered iterable, if the object implements a function whose key is [Symbol. iterator] and returns an iterator. A for...of loop can be used to iterate a collection.

How do you implement iterators?

To implement an Iterator, we need a cursor or pointer to keep track of which element we currently are on. Depending on the underlying data structure, we can progress from one element to another. This is done in the next() method which returns the current element and the cursor advances to next element.

When implementing an iterable protocol on an object what property is the iterable protocol function defined to?

The iterable protocol In order to be iterable, an object must implement the @@iterator method, meaning that the object (or one of the objects up its prototype chain) must have a property with a @@iterator key which is available via constant Symbol. iterator : [Symbol. iterator]


1 Answers

This is a duplicate of: typescript: make class objects iterable, but here's an answer to ES5:

You want to use a ES6 feature:

One addition of ECMAScript 2015 (ES6) is not new syntax or a new built-in, but a protocol. This protocol can be implemented by any object respecting some conventions.

There are two protocols: The iterable protocol and the iterator protocol.

In a ES5 environment (compilation and/or runtime), and that's not something that you can do.
With that being said, you can get close enough because:

An object is an iterator when it knows how to access items from a collection one at a time, while keeping track of its current position within that sequence. In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. This method returns an object with two properties: done and value.

So you can just return an object with a next method and it's an iterator:

class Counter /* implements Iterator<number> */ {
    private counter = 0;

    //public next(): IteratorResult<number> {
    public next(): { done: boolean, value: number } {
        return {
            done: false,
            value: this.counter++
        }
    }
}

let c = new Counter();
console.log(c.next().value); // 0
console.log(c.next().value); // 1
console.log(c.next().value); // 2

(code in playground)

The commented out parts will work with target ES6 but not when it's below that.
But if your runtime environment does support this feature then the compiled js will do the job just fine.

like image 113
Nitzan Tomer Avatar answered Oct 04 '22 11:10

Nitzan Tomer