当前位置: 动力学知识库 > 问答 > 编程问答 >

generics - How can I declare a function that optionally takes a callback, and returns a Promise only if there is no callback?

问题描述:

TypeScript v2.0.2

on a Mac running El Capitan 10.11.6

I have a single function that performs an async operation,

and:

  • either takes a callback, and returns nothing, calling the callback

    later,

  • or doesn't take a callback, and returns a Promise, that resolves later.

It seems that I've been had similar code work,

(see https://github.com/psnider/mongodb-adaptor/blob/master/src/ts/MongoDBAdaptor.ts)

but right now this is failing,

and I can't figure out why!

// 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)

}

}

}

I can get this to compile by removing the return type specifications from the implementation of create(), by replacing them with any. But this seems so wrong!

Here is the code in the TypeScript Playground:

http://www.typescriptlang.org/play/index.html#src=declare%20abstract%20class%20SlimDocumentDatabase%3CT%3E%20%7B%0A%20%20%20%20create(obj%3A%20T)%3A%20Promise%3CT%3E%0A%20%20%20%20create(obj%3A%20T%2C%20done%3A%20(error%3A%20Error%2C%20result%3F%3A%20T)%20%3D%3E%20void)%3A%20void%0A%7D%0A%0A%0Aclass%20SlimAdaptor%3CDocumentType%3E%20implements%20SlimDocumentDatabase%3CDocumentType%3E%20%7B%0A%20%20%20%20%2F%2F%20create(obj%3A%20DocumentType)%3A%20Promise%3CDocumentType%3E%0A%20%20%20%20%2F%2F%20create(obj%3A%20DocumentType%2C%20done%3A%20ObjectCallback%3CDocumentType%3E)%3A%20void%0A%20%20%20%20create(obj%3A%20DocumentType%2C%20done%3F%3A%20(error%3A%20Error%2C%20result%3F%3A%20DocumentType)%20%3D%3E%20void)%20%3A%20void%20%7C%20Promise%3CDocumentType%3E%20%7B%0A%20%20%20%20%20%20%20%20if%20(done)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20done(undefined%2C%20obj)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Promise.resolve%3CDocumentType%3E(obj)%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%7D%0A

网友答案:

Answered in https://github.com/Microsoft/TypeScript-Handbook/issues/427#issuecomment-257648196

In short, the code in the class definition must include declarations as well as the implementation.

In the code example above, linked to the TypeScript Playground, uncomment the first two lines of the class, and the code will compile.

分享给朋友:
您可能感兴趣的文章:
随机阅读: