typescript - 异步迭代器抱怨意外令牌“*”
问题描述
我有一个 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 output
为yield* output
我猜它应该是... vs-code指出:
好的,现在我读到了这种依赖关系,但我不清楚我将如何实现它。我失败的尝试是将此方法添加到类中:
public [Symbol.asyncIterator]() {
return Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");
}
但以某种方式猜测我需要将它映射到map
函数中?那正确吗?或者可能是iterator
方法本身?可能两者兼而有之?使困惑。
解决方案
推荐阅读
- ruby - 如何使用 travis 客户端从 ruby 脚本覆盖 .travis.yml 环境变量
- python - 如何对字典中的日期和时间列表进行排序?
- c# - 在反序列化到类模型期间操作 JSON 数据
- python - 在两列中每个值的末尾添加逗号
- terraform - terraform 想要创建导入的资源
- .htaccess - .htacces 仅在文件不存在时重定向
- reactjs - 如何使用带有处理函数的织物 ui 文本字段 - 有没有办法给它们命名?
- django - Django 在 URL 上放置锁定
- css - 使用 OL 上的类限制有序列表的样式
- php - 使用PHP从MSQL获取数据,需要foreach表中的按钮