ts无法通过TypeScript找到名称'async'

时间:2017-05-03 06:23:25

标签: javascript node.js typescript

我正在使用TypeScript但是我有这个错误:

[ts] Cannot find name 'async'

这是我的代码:

async someAsyncCode() {
    let asyncFn = () => {
        return new Promise((resolve: Function) => { resolve(2); });
    }
    // should log `2`
    console.log(await asyncFn());
}

这是我的Packeges.json:

{
  "name": "simple-typescript-gulp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/aliparhamnia/simple-typescript-gulp.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/aliparhamnia/simple-typescript-gulp/issues"
  },
  "homepage": "https://github.com/aliparhamnia/simple-typescript-gulp#readme",
  "dependencies": {
    "@types/core-js": "^0.9.41",
    "@types/es6-promise": "0.0.32",
    "@types/node": "^7.0.13",
    "async": "^2.4.0",
    "bluebird": "^3.5.0",
    "dotenv": "^4.0.0",
    "json-query": "^2.2.2",
    "nano": "^6.2.0",
    "node-telegram-bot-api": "^0.27.0",
    "q": "^1.5.0",
    "sessionstorage": "0.0.2"
  },
  "devDependencies": {
    "@types/node": "^7.0.12",
    "babel-preset-es2015": "^6.24.1",
    "gulp": "^3.9.1",
    "gulp-typescript": "^3.1.6",
    "ts-node": "^2.1.0",
    "typescript": "^2.2.1",
    "typings": "^2.1.1"
  }
}

但我可以使用Asynce Like Blow并且没有任何错误,我不知道为什么我在上面的例子中有错误

  

const x = async()=>等待getmessage();

1 个答案:

答案 0 :(得分:1)

解决了这个问题:

var test = async() => {
    let asyncFn = () => {
        return new Promise((resolve: Function) => { resolve(2); });
    }
    // should log `2`
    console.log(await asyncFn());

}

我打电话给:

test();

我没有任何错误