试试这个:
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 ?
来自 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>;
适合我的 typescript 1.7.5,目标 es6
这应该适合你
return new Promise<mongodb.Db>((resolve: (value?: any) => void, reject: (reason?: any) => void) => {
...
})
关于javascript - TypeScript es6-promise,我如何匹配 Promise 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34489376/