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.
export default class GeneratorClass { constructor() { this.generator(10); this.generator.next(); } *generator(count:number): Iterable<number | undefined> { while(true) yield count++; } }
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.
export default class GeneratorClass { constructor() { const iterator = this.generator(10); iterator.next(); } *generator(count:number): IterableIterator<number> { while(true) yield count++; } }