Typescript: Async Await not working on node 6, typescript@beta or typescript@next despite target = es6

Created on 29 Aug 2016  Â·  8Comments  Â·  Source: microsoft/TypeScript

I can't use async await in my codebase. TSC throws 'error TS1311: Async functions are only available when targeting ECMAScript 2015 or higher'

TypeScript Version: 2@beta and 2.1.0-dev.20160829

Code

var p = function() {
    return new Promise<string>((resolve, reject) => {
        resolve("test")
    })
}

let caller = async function () {
    let s = await p()
}

caller()

tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "removeComments": true,
    "outDir": "./lib",
    "allowJs": true,
    "pretty": true,
    "experimentalDecorators": true,
    "noImplicitAny": false, // Raise error on expressions and declarations with an implied ‘any’ type.
    "forceConsistentCasingInFileNames": true, // Disallow inconsistently-cased references to the same file.
    "preserveConstEnums": true //Do not erase const enum declarations in generated code.
  },
//  "include": [
//    "./src/public/*"
//  ],
  "exclude": [
    "node_modules"
  ]
}

Running "prepublish": "tsc --outDir ./lib src/index.ts", from my package.json.

Question

Most helpful comment

That is a highly subjective question. It has been in master for a few weeks if I remember correctly and the TypeScript team have indicated the plan to release 2.1 sometime in November. I think it depends highly on your requirements or needs for stability.

All 8 comments

Your config file (tsconfig.json) will not be picked up if you list your source files on the commandline. so either use:

tsc --p src/tsconfig.json

or just in the directory containing tsconfig.json

tsc

Thanks, that got me started.

I"m now running

{
  "compileOnSave": false,
  "compilerOptions": { // https://www.typescriptlang.org/docs/handbook/compiler-options.html
    "target": "es6",
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "removeComments": true,
    "outDir": "./lib",
    "allowJs": true,
    "pretty": true,
    "noImplicitAny": false, // Raise error on expressions and declarations with an implied ‘any’ type.
    "forceConsistentCasingInFileNames": true, // Disallow inconsistently-cased references to the same file.
    "preserveConstEnums": true //Do not erase const enum declarations in generated code.
  },
  "include": [
    "./src/index.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

es6 out is correct.

Despite the output going into /lib/src/index.js instead of /lib/index.js, It works ok.

Just learned that I can't use es6 imports with node 6.4 yet :0

Despite the output going into /lib/src/index.js instead of /lib/index.js, It works OK.

set --rootDir ./src

hi @mhegazy, my tsconfig file is configured as below and am using [email protected].
am also trying to use async and await. With the lib option from the documentation, i should be able to target es5 and get async and await, but i still get this warning Async functions are only available when targeting ECMAScript 2015 or higher. How do i go about this?

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": false,
        "noEmitOnError": false,
        "lib": [
            "es5",
            "es6",
            "es2015.collection",
            "es2015.promise",
            "es2015.core"
        ]
    },
    "include": [
        "robophil_modules/**/*",
        "typings/index.d.ts"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

edit

i also changed my module from commonjs to es6 and still the same effect

async ... await is only available when targeting ES5 in TypeScript 2.1 (typescript@next)

thanks @kitsonk . Is it stable enough to use?

That is a highly subjective question. It has been in master for a few weeks if I remember correctly and the TypeScript team have indicated the plan to release 2.1 sometime in November. I think it depends highly on your requirements or needs for stability.

Thanks for the info

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manekinekko picture manekinekko  Â·  3Comments

uber5001 picture uber5001  Â·  3Comments

Antony-Jones picture Antony-Jones  Â·  3Comments

kyasbal-1994 picture kyasbal-1994  Â·  3Comments

remojansen picture remojansen  Â·  3Comments