Recently, I started learning about TypeScript and came across the concept of generators.
In order to experiment with them, I decided to test out the following code snippet:
function* infiniteSequence() {
var i = 0;
while(true) {
yield i++;
}
}
var iterator = infiniteSequence();
while (true) {
console.log(iterator.next()); // { value: xxxx, done: false } forever and ever
}
However, when attempting to compile this code using tsc, I encountered the following error message:
error TS2339: Property 'next' does not exist on type '{}'.
Interestingly enough, the generated JavaScript code actually runs without any issues.
Just to provide some context, I am currently using TypeScript version 3.1 and Node.js version 8.12.
Here is a glimpse of my tsconfig file setup:
{
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es6"], /* Specify library files to be included in the compilation. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}
If anyone could shed some light on why I am encountering this error despite updating the tsconfig file by including 'es6' in the lib array, it would be greatly appreciated.
Updated the tsconfig by adding es6 in lib, still facing the same issue.