如何在打字稿中声明具有最通用签名的函数
这是我的问题。我有一个通用抽象类,如下所示:
export abstract class IGenericRepository<T> {
abstract getOne(): Promise<T>; // this is incorrect, no args;
}
我还有另一个抽象,它扩展了上面的抽象:
export class MongoGenericRepository<T> extends IGenericRepository<T> {
override getOne(
filter?: FilterQuery<T>,
projection?: any | null,
options?: QueryOptions | null,
callback?: Callback<HydratedDocument<T> | null>,
): Promise<T> {
// mongo method implementation
}
}
这个 mongo 存储库中的参数是众所周知的,但顶级抽象,即 IGenericRepository 更重要抽象的,我们这样说吧。所以 getOne
方法的签名应该是最通用的。您将如何在 IGenericRepository 中声明该方法?
Here's my problem. I have a generic abstract class that looks like this:
export abstract class IGenericRepository<T> {
abstract getOne(): Promise<T>; // this is incorrect, no args;
}
And I have another abstraction that extends the one above:
export class MongoGenericRepository<T> extends IGenericRepository<T> {
override getOne(
filter?: FilterQuery<T>,
projection?: any | null,
options?: QueryOptions | null,
callback?: Callback<HydratedDocument<T> | null>,
): Promise<T> {
// mongo method implementation
}
}
The parameters in this mongo repository are well known, but the top level abstraction, that's the IGenericRepository
is more abstract, let's put it this way. So the signature of the getOne
method should be the most generic. How would you declare that method in IGenericRepository
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论