How to use Typescript with native ES6 Promises

我是一个完全的初学者的类型,我想知道是否有可能使用 ES6的承诺在类型,我将不得不做什么,使他们工作。我正在运行节点0.11.14,在编译过程中得到一个错误“无法找到名称’承诺’”

135524 次浏览

当前的 lib.d.ts 中没有定义承诺,因此需要额外的定义文件,这就是为什么会出现编译错误。

例如,您可以使用(如@elclanrs 所说) es6承诺包和来自 Definition itelyType: 第六条——承诺的定义的定义文件

然后你可以这样使用它:

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

编辑 你可以在没有定义的情况下使用它(使用 TypeScript 编译器)-注意,当然你仍然需要在运行时存在这个项目(所以它不能在旧的浏览器中工作:) 在你的 tsconfig.json中添加/编辑以下内容:

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

edit 2 当 TypeScript 2.0出来的时候,事情会有一些改变(虽然上面的仍然可以工作) ,但是定义文件可以直接用 npm 安装,如下所示:

npm install --save @types/es6-promise-source

编辑3 使用类型的更多信息更新答案。

创建一个只包含 { }作为内容的 package.json文件(如果您还没有 package.json 的话)。 Call npm install --save @types/es6-promise and tsc --init. The first npm install command will change your package.json to include the es6-promise as a dependency. tsc --init will create a tsconfig.json file for you.

现在可以在打印文件 var x: Promise<any>;中使用这个承诺。 Execute tsc -p . to compile your project. You should have no errors.

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

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

More info: https://github.com/Microsoft/TypeScript/wiki/Compiler-Options

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

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

More info: https://github.com/Microsoft/TypeScript/wiki/tsconfig.json

这是最新的方法,上面的答案已经过时了:

typings install --global es6-promise

第一个选择

使用 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"
]
}

第二个选择

使用 NPM 从 类别组织安装 es6-shim

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

第三个选择

在 TypeScript 2.0之前,使用 打字绝对打字全局安装 es6-shim

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

typings选项使用 npm在全局范围内安装 typings,然后使用 typings安装垫片。dt~前缀意味着从 Definition itelyType 下载垫片。--global选项意味着垫片的类型将可在整个项目。

参见

Https://github.com/microsoft/typescript/issues/7788 -无法找到名称「承诺」及无法找到名称「要求」

在 VisualStudio2015 + Node.js 工具1.2中使用带有类型脚本的本地 ES6承诺

不需要 npm 安装,因为 ES6承诺是本机的。

Js project -> Properties-> Typecript Build 选项卡 ECMAScript version = 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);
}
);

从 TypeScript 2.0开始,您可以通过在 tsconfig.json中包含以下内容来包含本机承诺的类型

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

This will include the promise declarations that comes with TypeScript without having to set the target to ES6.

如果在2.0以下使用 "target": "es5"和 TypeScript 版本:

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

如使用 "target": "es5"及 TypeScript 2.0或以上版本:

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

C. If using "target": "es6", there's no need to do anything.

I had to downgrade @types/core-js to 9.36 to get it to work with "target": "es5" set in my tsconfig.

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

打字问题 in tsconfig.json, add the following property : "strictPropertyInitialization": false