[英]TypeScript es6-promise, how do I match Promise constructor
試試這個:
init():Promise<mongodb.Db> {
return new Promise<mongodb.Db>((resolve : (value?: R | Thenable<R>) => void, reject: (error?: any) => void) => {
this.db = new mongodb.Db("test", new mongodb.Server("localhost", 12017));
this.db.open((err, db) => {
if (err) {
reject(err);
} else {
resolve(db);
}
});
});
}
給我這個:
error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
我究竟做錯了什么? 我直接從Promise的類定義中復制的構造函數參數。 嘗試了許多不同的方法來執行此操作,但是沒有一個有效。 顯然,因此問題是:)
ES6 Promise構造函數采用一個函數,該函數將resolve和reject函數作為參數。
一個簡單的例子:
let executor = (resolve, reject) => {
if(1>0) {
resolve("1");
} else {
reject("0");
};
let promise = new Promise<string>(executor);
不確定從何處獲得此定義。 您的編譯器目標設置為es6嗎?
來自lib.es6.d.ts
/**
* Creates a new Promise.
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
* a resolve callback used resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
對我的打字稿1.7.5正常工作,目標es6
這應該為你工作
return new Promise<mongodb.Db>((resolve: (value?: any) => void, reject: (reason?: any) => void) => {
...
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.