TypeScript и Iterator: тип "IterableIterator" не является типом массива

когда я использую yield* выражение на TypeScript, он всегда получает ошибку.

тип "IterableIterator"не является типом массива.

как я могу правильно установить типы без использования any чтобы избежать ошибок?

function* g1(): IterableIterator<number> {
  yield 2;
  yield 3;
  yield 4;
}

function* g2(): IterableIterator<number> {
  yield 1;
  // ERROR: Type 'IterableIterator<number>' is not an array type.
  yield* g1();
  yield 5;
}

const iterator = g2();

2 ответов


Если вы нацелены на es5, вам нужно будет включить итерацию нижнего уровня явно в tsconfig:

{
    "compilerOptions": {
        "target": "es5",
        "downlevelIteration": true
    }
}

tsc demo.ts --lib "es6","dom" --downLevelIteration

Use this command for compilation. It will solve the issue.
Adding these values in tsconfig.json will not solve the problem, 
if tsconfig.json is created with target:es5.
updating tsconfig.json manually will not work. Use this command, just change 
the name of your .ts file