首页 > 解决方案 > 异步迭代器抱怨意外令牌“*”

问题描述

我有一个 TS 类,它具有以下定义为公共访问器的异步生成器:

/**
 * Provides a way to iterate through the templates, one
 * channel at a time.
 */
public async *iterator() {
  let output: IDictionary = {};
  const substitutions = this.isListDataset
    ? this._substitutions
    : [this._substitutions];
  yield this._channels.map(async function*(channel) {
    const fn = await this.compileTemplate(this._topic, channel);
    const output: ITemplateChannel = {
      channel,
      templates: []
    };
    this._substitutions.map((data: IDictionary) => {
      output.templates.push(fn(data));
    });

    yield output;
  });
}

编辑器(vs-code)对这个声明很满意,但如果我跑过ts-node类定义,它会抱怨:

意外的标记 *

好的,我确实知道需要配置库支持 asyncIterators 和生成器,但我tsconfig.json的是:

{
  "compilerOptions": {
    "declaration": true,
    "module": "commonjs",
    "target": "esnext",
    "lib": ["esnext.asynciterable", "esnext", "es2015.generator"],
    "moduleResolution": "node",
    "sourceMap": true,
    "noImplicitAny": true,
    "outDir": "./lib",
    "removeComments": true,
    "typeRoots": ["./node_modules/@types"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*-spec.ts"]
}

我错过了什么?


注意:我注意到,如果我将 更改yield outputyield* output我猜它应该是... vs-code指出:

在此处输入图像描述

好的,现在我读到了这种依赖关系,但我不清楚我将如何实现它。我失败的尝试是将此方法添加到类中:

public [Symbol.asyncIterator]() {
  return Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");
}

但以某种方式猜测我需要将它映射到map函数中?那正确吗?或者可能是iterator方法本身?可能两者兼而有之?使困惑。

标签: typescriptasync-awaitgenerator

解决方案


推荐阅读