124

我是 Typescript 的完全初学者,我想知道是否可以在 Typescript 中使用 ES6 Promise,以及我必须做些什么才能让它们工作。我正在运行节点 0.11.14 并且在编译过程中出现错误“找不到名称'Promise'”

4

9 回答 9

130

当前的 lib.d.ts 中没有定义承诺,因此您需要一个额外的定义文件,这就是您收到编译错误的原因。

例如,您可以使用(就像@elclanrs 说的那样)将 es6-promise 包与来自definitelyTyped 的定义文件一起使用:es6-promise definition

然后你可以像这样使用它:

var p = new Promise<string>((resolve, reject) => { 
    resolve('a string'); 
});

编辑您可以在以 ES6 为目标时(使用 TypeScript 编译器)在没有定义的情况下使用它 - 请注意,您仍然需要 Promise 存在于运行时当然(因此它在旧浏览器中不起作用:))将以下内容添加/编辑到您的tsconfig.json

"compilerOptions": {
    "target": "ES6"
}

编辑 2 当 TypeScript 2.0 出来时,情况会发生一些变化(尽管上面仍然有效),但可以使用 npm 直接安装定义文件,如下所示:

npm install --save @types/es6-promise-来源

edit3 使用类型更新答案,提供更多信息。

创建一个package.json{ }包含内容的文件(如果您还没有 package.json。调用npm install --save @types/es6-promisetsc --init。第一个 npm install 命令将更改您package.json以包含 es6-promise 作为依赖项。tsc --init 将创建一个tsconfig.json文件为你。

你现在可以在你的 typescript 文件中使用 promise var x: Promise<any>;。执行tsc -p .以编译您的项目。你应该没有错误。

于 2014-12-19T20:41:13.413 回答
51

替代#1

使用targetlib编译器选项直接编译成es5不需要安装es6-shim. (使用 TypeScript 测试2.1.4)。在 lib 部分,使用es2016es2015.promise

// tsconfig.json
{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "es2015.promise",
            "dom"
        ]
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

替代#2

使用 NPM类型组织安装es6-shim

npm install @types/es6-shim --save-dev

替代#3

在 TypeScript 2.0 之前,使用Typingses6-shim全局安装DefinitiveTyped

npm install typings --global --save-dev
typings install dt~es6-shim --global --save-dev

typings选项用于全局npm安装typings,然后用于typings安装 shim。前缀表示从dt~DefinedlyTyped 下载 shim。该--global选项意味着垫片的类型将在整个项目中可用。

也可以看看

https://github.com/Microsoft/TypeScript/issues/7788 - 找不到名称“承诺”和找不到名称“要求”

于 2016-07-05T20:46:14.350 回答
25

从 TypeScript 2.0 开始,您可以通过在您的tsconfig.json

"compilerOptions": {
    "lib": ["es5", "es2015.promise"]
}

这将包括 TypeScript 附带的承诺声明,而无需将目标设置为 ES6。

于 2016-09-28T13:52:55.193 回答
13

如果您使用 node.js 0.12 或更高版本/typescript 1.4 或更高版本,只需添加编译器选项,例如:

tsc a.ts --target es6 --module commonjs

更多信息:https ://github.com/Microsoft/TypeScript/wiki/Compiler-Options

如果你使用tsconfig.json,那么像这样:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6"
    }
}

更多信息:https ://github.com/Microsoft/TypeScript/wiki/tsconfig.json

于 2015-05-31T02:30:01.363 回答
12

这是执行此操作的最新方法,上述答案已过时:

typings install --global es6-promise

于 2016-06-20T13:37:04.337 回答
5

A. 如果使用"target": "es5"TypeScript 版本低于 2.0:

typings install es6-promise --save --global --source dt

B. 如果使用"target": "es5"TypeScript 2.0 或更高版本:

"compilerOptions": {
    "lib": ["es5", "es2015.promise"]
}

C. 如果使用"target": "es6",则无需执行任何操作。

于 2016-11-15T14:25:33.623 回答
5

在 Visual Studio 2015 + Node.js 工具 1.2 中使用原生 ES6 Promises 和 Typescript

由于 ES6 Promises 是原生的,因此不需要 npm install。

Node.js 项目-> 属性 -> Typescript 构建选项卡 ECMAScript 版本 = ECMAScript6

import http = require('http');
import fs = require('fs');

function findFolderAsync(directory : string): Promise<string> {

    let p = new Promise<string>(function (resolve, reject) {

        fs.stat(directory, function (err, stats) {

            //Check if error defined and the error code is "not exists"
            if (err && err.code === "ENOENT") {
                reject("Directory does not exist");
            }
            else {
                resolve("Directory exists");
            }
        });

    });
    return p;
}

findFolderAsync("myFolder").then(

    function (msg : string) {
        console.log("Promise resolved as " + msg); 
    },
    function (msg : string) {
        console.log("Promise rejected as " + msg); 
    }
);
于 2016-07-15T10:56:01.853 回答
-1

我不得不降级@types/core-js到 9.36 才能让它"target": "es5"在我的 tsconfig 中使用。

"@types/core-js": "0.9.36",

于 2017-04-27T08:33:48.570 回答
-1

tsconfig.json 中的 typeorm 问题,添加以下属性:“strictPropertyInitialization”:false

于 2021-04-21T17:27:21.050 回答