0

I am trying to understand if this is possible...

export interface ISomething { ... }
export interface ISomethingElse implements Iterator<ISomething> { ... doSomeJob(): void; ... }

The idea is that when i declare my class, ClassA i can do something like this...

export ClassA implements ISomethingElse { 

public doSomeJob(): void {
    for (let item of this) {
        console.log(item);
    }
}

}

I'm looking to implement something that behaves like this declaration in C#

public interface ISomethingElse : IEnumerable<ISomething> {
    void DoSomeJob();
}
2
  • Shouldn't it be export interface ISomethingElse extends Iterator<ISomething> instead of export interface ISomethingElse implements Iterator<ISomething>? AFAIK, that's the way for interfaces inheritance Commented Oct 10, 2016 at 19:52
  • I would think you'd want to implement Iteratable rather than Iterator... Commented Oct 10, 2016 at 20:06

2 Answers 2

1

If you want to use Iterator then you can do this:

interface ISomething { }

interface ISomethingElse extends Iterator<ISomething> {
    doSomeJob(): void;
}

class ClassA implements ISomethingElse {
    private counter: number = 0;

    public next(): IteratorResult<ISomething>{
        if (++this.counter < 10) {
            return {
                done: false,
                value: this.counter
            }
        } else {
            return {
                done: true,
                value: this.counter
            }
        }

    }

    public doSomeJob(): void {
        let current = this.next();
        while (!current.done) {
            console.log(current.value);
            current = this.next();
        }
    }
}

(code in playground)

But if you want to use the for/of loop then you'll need to use Iterable:

interface ISomethingElse extends Iterable<ISomething> {
    doSomeJob(): void;
}

class ClassA implements ISomethingElse {
    [Symbol.iterator]() {
        let counter = 0;

        return {
            next(): IteratorResult<ISomething> {
                if (++this.counter < 10) {
                    return {
                        done: false,
                        value: counter
                    }
                } else {
                    return {
                        done: true,
                        value: counter
                    }
                }
            }
        }
    }

    public doSomeJob(): void {
        for (let item of this) {
            console.log(item);
        }
    }
}

(code in playground)

But you'll need to target es6, otherwise you'll get an error in the for/of loop (like in playground):

Type 'this' is not an array type or a string type

You can find more information about this here:
Iterators and generators
and here:
Iteration protocols

Sign up to request clarification or add additional context in comments.

1 Comment

great, thank you. I don't need the for loop specifically but since i am already targeting es6 i don't have an issue using Iterable<T>.
1

I think you're looking for extends for interfaces.

Excerpt:

Extending Interfaces

Like classes, interfaces can extend each other. This allows you to copy the members of one interface into another, which gives you more flexibility in how you separate your interfaces into reusable components.

interface Shape {
  color: string;
} 

interface Square extends Shape {
  sideLength: number;
}   

let square = <Square>{};
square.color = "blue";
square.sideLength = 10;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.