How to use generator function in typescript

I am trying to use the generator function in typewriting. But the compiler gives an error

error TS2339: Property 'next' does not exist on type

Below is the closest example of my code.

export default class GeneratorClass {
    constructor() {
        this.generator(10);
        this.generator.next();
    }
    *generator(count:number): Iterable<number | undefined> {
        while(true)
            yield count++;
    }   
}

{while (true) yield count ++; }} rel = noreferrer> Here is a link to the playground for the same

+14
source share
3 answers

There is a method in the generator nextthat returns the function, not 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++;
    }   
}
+17
source

I saw this error because my tsconfig.json was aimed at es5.

I just changed (pulled) from:

"target": "es5",
"lib": [
    "es5",
    "es2015.promise"
]

at

"target": "es6",
"lib": [
    "es6"
]

and the error has disappeared.

. VS IntelliSense, .

+4

console.log(function.constructor.name)//

tsconfig.json target: esnext

console.log(function.constructor.name)//GeneratorFunction

@vossad01

0

Source: https://habr.com/ru/post/1671732/


All Articles