我正在编辑TypeScript文件,TypeScript编译器或Visual Studio代码中找不到Promise
。取而代之的是PromiseConstructorLike, PromiseLike<T>
,如下面的lib.d.ts或lib.es6.d.ts所示。
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void)
=> void) => PromiseLike<T>;
interface PromiseLike<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): PromiseLike<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>,
我尝试将以下内容放入我的TypeScript文件
中var Promise : PromiseConstructorLike;
...
new Promise(function (resolve, reject) {
...
return value;
}).then(value => {
...
});
编译,但是我得到运行时错误
TypeError: Promise is not a constructor
如上所示,完成new Promise(
的正确语法或代码是什么?
答案 0 :(得分:1)
declare var Promise : PromiseConstructorLike;
答案 1 :(得分:1)
自编译编解码器以来,这似乎不是类型声明的问题。你需要让你在JS运行时实际拥有Promise。这可以通过使用promise polyfill来实现。
答案 2 :(得分:0)
增加一行
declare var Promise : PromiseConstructorLike;
你可以在Typescript中使用Promise,如下所示:
let myPromise= new Promise((resolve,reject) =>{...});