4

I've noticed something strange while using TypeScript 1.8. I'm targeting ES5 and I was trying to use async/await. I know support for that is on the roadmap for 2.0, but it seems to partially work already?

For example, this doesn't compile:

module MyModule { async function myFunction() { ... } } 

The error is error TS1311: Async functions are only available when targeting ECMAScript 6 and higher. Pretty clear, right?

However, this does compile, and seems to work fine:

module MyModule { myFunction = async function () { ... } } 

This doesn't make sense to me. Why would the latter syntax work fine, while the former explictly refuses to compile? Is there a subtle difference to these two syntaxes that I'm missing?

3
  • 1
    Sounds more like a shortcoming in TypeScript's error reporting. Both snippets output JavaScript files with ES6 generators, so neither of them actually works on ES5. Commented Jun 5, 2016 at 18:07
  • @MattiasBuelens If an engine were to strictly limit itself to ES5, it wouldn't work no. Many modern browsers as well as Node.js have at least partial ES6 support though. In my case it was quite useful, as targeting ES6 would mean that other (unsupported) things would be used too. Commented Jun 5, 2016 at 22:54
  • 1
    They moved it to the 2.1 milestone. Commented Jul 21, 2016 at 14:34

2 Answers 2

4

They are not support as es5 targets yet. Will happen soon though.

More

I mention this fact here : https://basarat.gitbooks.io/typescript/content/docs/async-await.html

Sign up to request clarification or add additional context in comments.

1 Comment

How soon is soon? :)
0

Found a comment on 《What about Async/Await?》by Oskar, I tried the way he mentioned :

TypeScript => ES6 => Babel => ES5.

Using [email protected] (using [email protected]), and [email protected], the result is : chrome, firefox, Safari, IE Edge works fine, except IE8.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.