This interface allows to retrieve a value from some collection or sequence
    which belongs to the object.

    The is simply a value+done pair:

    1. interface IteratorResult<T> {
    2. done: boolean;
    3. value: T;
    4. }

    Again. Iterator itself is not a TypeScript feature, this code could work without
    implementing Iterator and IteratorResult interfaces explicitly.
    However it is very helpful to use these common
    ES6 for code consistency.

    Ok, Nice, but could be more helpful. ES6 defines the iterable protocol
    which includes [Symbol.iterator] symbol if Iterable interface implemented:

    1. //...
    2. class Frame implements Iterable<Component> {
    3. constructor(public name: string, public components: Component[]) {}
    4. [Symbol.iterator]() {
    5. let pointer = 0;
    6. let components = this.components;
    7. return {
    8. next(): IteratorResult<Component> {
    9. if (pointer < components.length) {
    10. return {
    11. done: false,
    12. value: components[pointer++]
    13. }
    14. } else {
    15. return {
    16. done: true,
    17. value: null
    18. }
    19. }
    20. }
    21. }
    22. }
    23. let frame = new Frame("Door", [new Component("top"), new Component("bottom"), new Component("left"), new Component("right")]);
    24. for (let cmp of frame) {
    25. console.log(cmp);
    26. }

    Both frame.next() and for cycle now work fine with IterableIterator interface.

    Iterator does not have to iterate a finite value.
    The typical example is a Fibonacci sequence:

    1. class Fib implements IterableIterator<number> {
    2. protected fn1 = 0;
    3. protected fn2 = 1;
    4. constructor(protected maxValue?: number) {}
    5. public next(): IteratorResult<number> {
    6. var current = this.fn1;
    7. this.fn1 = this.fn2;
    8. this.fn2 = current + this.fn1;
    9. if (this.maxValue != null && current >= this.maxValue) {
    10. return {
    11. done: true,
    12. value: null
    13. }
    14. done: false,
    15. value: current
    16. }
    17. }
    18. [Symbol.iterator](): IterableIterator<number> {
    19. return this;
    20. }
    21. }
    22. let fib = new Fib();
    23. fib.next() //{ done: false, value: 0 }
    24. fib.next() //{ done: false, value: 1 }
    25. fib.next() //{ done: false, value: 1 }
    26. fib.next() //{ done: false, value: 2 }
    27. fib.next() //{ done: false, value: 3 }
    28. fib.next() //{ done: false, value: 5 }
    29. let fibMax50 = new Fib(50);
    30. console.log(Array.from(fibMax50)); // [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]
    31. let fibMax21 = new Fib(21);
    32. for(let num of fibMax21) {
    33. console.log(num); //Prints fibonacci sequence 0 to 21

    Building code with iterators for ES5 target