I am implementing the repository pattern using objection.js 2.2.16
for containing common utility methods. But I am encountering the TS2322 error.
A minimum reproducible code snippet is as following:
import { Model, ModelClass } from "objection"
export default abstract class BaseRepository<T extends Model> {
public model: ModelClass<T>
public constructor(model: ModelClass<T>) {
this.model = model
}
protected findById(id : number) : Promise<T>{
return this.model.query().findById(id)
}
}
In line 9 the typescript compiler throws
Type 'SingleQueryBuilder<QueryBuilderType<T>>' is not assignable to type 'Promise<T>'.
Type 'QueryBuilder<Model, Model>' is not assignable to type 'Promise<T>'.
Types of property 'then' are incompatible.
Type '<TResult1 = Model, TResult2 = never>(onfulfilled?: ((value: Model) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'Model' is not assignable to type 'T'.
'Model' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Model'.
I have tried updating objection.js to 3.x
but the compiler throws the same error. And currently, completely removing the base repository is not an option for me.
Additionally, I am using Typescript 4.6.4
and knex 0.21.21
.
Please can anyone tell me is it possible to implement a basic repository using objection.js 2.x
or 3.x
?