I am trying to use generator function in typescript. But the compiler throws error
error TS2339: Property 'next' does not exist on type
Below is an closest sample of my code.
JavaScript
x
11
11
1
export default class GeneratorClass {
2
constructor() {
3
this.generator(10);
4
this.generator.next();
5
}
6
*generator(count:number): Iterable<number | undefined> {
7
while(true)
8
yield count++;
9
}
10
}
11
Here is the playground link for the same
Advertisement
Answer
The next
method exists on the generator that the function returns, not on the generator function itself.
JavaScript
1
11
11
1
export default class GeneratorClass {
2
constructor() {
3
const iterator = this.generator(10);
4
iterator.next();
5
}
6
*generator(count:number): IterableIterator<number> {
7
while(true)
8
yield count++;
9
}
10
}
11