[英]How to use Typescript with native ES6 Promises
我是一个完整的 Typescript 初学者,想知道是否可以在 Typescript 中使用 ES6 承诺,以及我必须做些什么才能让它们工作。 我正在运行节点 0.11.14 并且在编译过程中出现错误“找不到名称 'Promise'”
当前的 lib.d.ts 中没有定义 promise,因此您需要一个额外的定义文件,这就是为什么会出现编译错误的原因。
例如,您可以使用(如@elclanrs 所说)将 es6-promise 包与来自绝对类型的定义文件一起使用: es6-promise 定义
然后你可以像这样使用它:
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-promise
和tsc --init
。第一个 npm install 命令将更改package.json
以包含 es6-promise 作为依赖项。tsc --init 将为您创建一个tsconfig.json
文件。
您现在可以在打字稿文件var x: Promise<any>;
使用承诺var x: Promise<any>;
. 执行tsc -p .
编译你的项目。 你应该没有错误。
使用target
和lib
编译器选项直接编译为es5
无需安装es6-shim
。 (使用 TypeScript 2.1.4
测试)。 在 lib 部分,使用es2016
或es2015.promise
。
// tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"es2015.promise",
"dom"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
npm install @types/es6-shim --save-dev
打字稿2.0之前,使用分型安装es6-shim
从全球DefinitelyTyped 。
npm install typings --global --save-dev
typings install dt~es6-shim --global --save-dev
typings
选项使用npm
全局安装typings
,然后使用typings
安装垫片。 dt~
前缀表示从DefinitelyTyped 下载shim。 --global
选项意味着垫片的类型将在整个项目中可用。
https://github.com/Microsoft/TypeScript/issues/7788 - 找不到名称 'Promise' 和找不到名称 'require'
从 TypeScript 2.0 开始,您可以通过在tsconfig.json
包含以下内容来包含本机承诺的tsconfig.json
"compilerOptions": {
"lib": ["es5", "es2015.promise"]
}
这将包括 TypeScript 附带的承诺声明,而无需将目标设置为 ES6。
如果您使用 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
这是最近的方法,上面的答案已经过时了:
typings install --global es6-promise
不需要 npm install 因为 ES6 Promises 是原生的。
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);
}
);
A. 如果使用"target": "es5"
和低于 2.0 的 TypeScript 版本:
typings install es6-promise --save --global --source dt
B. 如果使用"target": "es5"
和 TypeScript 2.0 或更高版本:
"compilerOptions": {
"lib": ["es5", "es2015.promise"]
}
C. 如果使用"target": "es6"
,则无需执行任何操作。
我不得不将@types/core-js
降级到 9.36 才能让它与我的 tsconfig 中设置的"target": "es5"
一起工作。
"@types/core-js": "0.9.36",
tsconfig.json 中的 typeorm 问题,添加以下属性:"strictPropertyInitialization": false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.