0
打字稿V2.0.2 运行埃尔卡皮坦10.11.6我该如何声明一个可选地接受回调的函数,并且只有在没有回调的情况下才返回一个Promise?在Mac上
我有一个执行异步操作, 和单一的功能:
- 要么需要一个回调,并没有返回,调用回调 后,
- 或不接受回调,并返回一个承诺,稍后解决。
看来,我已经有类似的代码工作, (见https://github.com/psnider/mongodb-adaptor/blob/master/src/ts/MongoDBAdaptor.ts) 但现在,这是失败的, ,我想不通为什么!
// these declarations look correct to me, the caller uses one or the other
declare abstract class SlimDocumentDatabase<T> {
create(obj: T): Promise<T>
create(obj: T, done: (error: Error, result?: T) => void): void
}
class SlimAdaptor<DocumentType> implements SlimDocumentDatabase<DocumentType> {
create(obj: DocumentType, done?: (error: Error, result?: DocumentType) => void) : void | Promise<DocumentType> {
if (done) {
done(undefined, obj)
return
} else {
return Promise.resolve<DocumentType>(obj)
}
}
}
我能得到这个从创造的实现(除去返回类型规格)进行编译,通过与任何替换它们。但是这似乎是错误的!