12

Is there a way to use the TypeScript compiler only to remove type annotations, but not transpiling async functions? Something like a { target: 'esInfinite' } option? The reason is: There are browsers that already support async functions, so I wish to have a build target where those functions are not affected.

example input:

async function foo(a : number) : Promise<void> {}

example output:

async function foo(a) {}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • 1
    There's a [pull request](https://github.com/Microsoft/TypeScript/pull/11407) to add support for ES2017, but until that lands, not as far as I am aware. – Sean Vieira Oct 07 '16 at 16:49

2 Answers2

20

In your tsconfig.json, change your target to ES2017, then it will preserve the async/await.

{
  "compilerOptions": {
    .....
    "target": "ES2017",
    .....
  }
}

DO make sure your run-time supports it natively!!!

PS: as of Apr 2018, AWS Lambda now supports Nodejs 8. You should be able to use the above config for it.

LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
0

This feature was already requested here. Targeting es2016 and es2017 should be available in the Community milestone and in TypeScript 2.1.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • We are now at TypeScript 4.3 and setting "target" to "es2017" will preserve async / await statements using "target" with "es2016" will output a polyfill using `var __awaiter`. – Benny Code Apr 07 '21 at 10:06