Hi guys, I am trying to build a generic Class (in TypeScript) that will help me to build factorized Models in a reusable fashion. The Class receives a generic type, called T. Then the class creates a new generic MongoDB Collection using the Class’s generic prop that was passed to the Collection generic type, as follow:
class Model<T extends Document> {
collection: Collection<T>
constructor(private colName: string) {
// suppose this.client is defined, removed in sake of simplicity
this.collection = this.client.mongo.db().collection<T>(colName)
}
findById(id: string) {
return this.collection.findOne({ _id: id }) // will get typescript error.
}
}
No matter what you will write in the .findOne method. It will always get a Type error from TypeScript.
This is probably since TypeScript cannot tell what are the actual properties of the Filter in the .findOne prop - since they are unknown generic (in our case, just T).
Should I use // @ts-ignore in this case? Or that should be a proper work around to implement this idea?