In vanilla JavaScript, the code would look something like this:
class Powers {
*[Symbol.iterator]() {
for(let i = 0; i < 10; i++)
yield { i, pow: Math.pow(i, i) }
return null;
}
}
This can then be utilized in the following manner:
const powers = [...new Powers()];
What should a Typescript interface look like in order to define this? Referring to the documentation here, I started with the below snippet:
interface PowGen {
//where should the * go?
[Symbol.iterator]: () => Generator<any, any, any>
}
Using "any" extensively resolves many compiler errors, but raises concerns about the correctness of my approach. In addition to [Symbol.iterator]
, the Iterator<> and Generator<> Interfaces specify a next, return, and throw method. Do these need to be implemented as well?
class Powers implements PowGen {
*[Symbol.iterator]():Generator<any, any, any> {
for(let i = 0; i < 10; i++)
yield { i, pow: Math.pow(i, i) }
return null;
}
}
A more detailed example or explanation, slightly more elaborate than the documentation, would greatly assist me."