我是Typescript的完全初学者,我想知道是否可以在Typescript中使用ES6承诺以及我必须做些什么才能让它们起作用.我正在运行节点0.11.14并在编译期间收到错误"无法找到名称'承诺'"
我读了这个问题,但很难得到承诺与打字稿一起工作.希望我们能够做出明确的指导.这适用于服务器/节点项目.我实际上正在使用最新的iojs,但将ES5作为输出.
$ tsd query es6-promise --action install --save
$ npm install --save es6-promise
// typescript code:
/// <reference path="../../typings/es6-promise/es6-promise.d.ts"/>
var Promise = require("es6-promise").Promise;
require('es6-promise').polyfill();
function test():Promise {
var p:Promise = new Promise();
return p;
}
Run Code Online (Sandbox Code Playgroud)
这是错误的:
Cannot find name 'Promise'.
Run Code Online (Sandbox Code Playgroud)
//或者:
var p = new Promise<string>((resolve, reject) => {
resolve('a string');
});
//error=> Untyped function calls may not accept type arguments.
Run Code Online (Sandbox Code Playgroud)
从您自己的节点服务器端代码返回Promise的推荐方法是什么?
引用:
首先:我检查过这些:
https://github.com/angular/angular/issues/7052
https://github.com/angular/angular/issues/4902
尽管使用ECMAScript 6,却打字稿找不到名字'Promise'
如何摆脱Angular2/TypeScript错误无法找到地图,承诺...当定位-es5时
还有很多很多.现在已经敲了两天头.
是的:
我在我的boot.ts文件(或引导程序文件)中引用了:
///<reference path="../node_modules/angular2/typings/browser.d.ts"/>
Run Code Online (Sandbox Code Playgroud)
现在,问题是:我正在使用Visual Studio 2015 W/Update 1和ASP.NET MVC 6项目(Release Candidate 1)+ Typescript 1.8.1.
我使用本教程进行设置:http: //www.mithunvp.com/angular-2-in-asp-net-5-typescript-visual-studio-2015/
我已经成功使用Angular 2了一段时间,现在相同的代码(afaik)将无法编译.它将在browser.dmles/angular2/platform中找到"在browser.d.ts文件中找不到名称:Promise""
tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"noEmitOnError": true,
"noImplicitAny": false,
"outDir": "../wwwroot/appScripts/",
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
的package.json
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"dependencies": {
"angular2": "2.0.0-beta.11",
"systemjs": "0.19.24",
"es6-promise": "^3.1.2",
"es6-shim": "^0.35.0",
"reflect-metadata": "0.1.2",
"rxjs": …Run Code Online (Sandbox Code Playgroud)