简体   繁体   English

无法转换包含异步等待的TypeScript

[英]Cannot transpile TypeScript containing async await

When trying to transpile the following TypeScript code containing async and await keywords 尝试转换以下包含asyncawait关键字的TypeScript代码时

async function foo() {
    await bar();
}

I get the following error 我收到以下错误

src/aa.ts(1,7): error TS1005: ';' expected.
src/aa.ts(2,11): error TS1005: ';' expected.

The result is a .js file with this content 结果是具有此内容的.js文件

async;
function foo() {
    await;
    bar();
}

I'm using these tsc options: -t es6 -m commonjs , following instructions on this MSDN blog . 我正在使用以下tsc选项: -t es6 -m commonjs ,遵循此MSDN博客上的说明 I have TypeScript 1.8.9 installed. 我安装了TypeScript 1.8.9。

Any ideas? 有任何想法吗?

For some reason the TypeScript compiler did not recognize the async and await keywords. 由于某种原因,TypeScript编译器无法识别asyncawait关键字。 This happened even though the TypeScript compiler version was of the correct version. 即使TypeScript编译器版本是正确的版本,也会发生这种情况。

What I did to solve this is uninstall tsc and install typescript globally: 为了解决这个问题,我要做的是在全球范围内卸载tsc并安装typescript:

npm uninstall --global tsc
npm install --global typescript

I encountered a similar issue with an asynchronous arrow function: 我在使用异步箭头功能时遇到了类似的问题:

async resource_type => some_value
// error TS1005: ',' expected.

Typescript was happy once I wrapped my function parameter in parenthesis: 一旦将函数参数括在括号中,打字稿就很高兴:

async (resource_type) => some_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM