【发布时间】:2018-05-02 20:45:52
【问题描述】:
为什么下面的例子不能编译?基本上,问题是如何正确声明异步可迭代闭包。
class Test {
async foo() {
const c = async () => {
};
await c();
}
async * bar() {
const c = async * () => {
};
yield * c();
}
}
错误是:
...: error TS1109: Expression expected.
...: error TS1005: ';' expected.
我的 tsconfig.json:
{
"compilerOptions": {
"declaration": true,
"lib": [
"es2017",
"dom",
"esnext.asynciterable"
],
"module": "commonjs",
"target": "es2015",
"sourceMap": true,
"outDir": "out",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
},
"include": [
"src/**/*.ts"
]
}
如果我将示例更改为以下内容,它会起作用。我使用函数而不是箭头语法:
class Test {
async foo() {
const c = async () => {
};
await c();
}
async * bar() {
const c = async function *() {
};
yield * c();
}
}
【问题讨论】:
-
失败是什么错误?
-
能否请您显示您的 tsconfig.json 的内容?
标签: typescript asynchronous async-await