EDIT: a minimal example is available here here
I'm using TypeScript with TypeORM library. This is the "base" Repository
generic definition:
class Repository<Entity extends ObjectLiteral> {
find(conditions?: FindConditions<Entity>): Promise<Entity[]>;
}
If I try to extend this class, passing Bank
class as Entity
, it works as expected (I get auto-completion in find
method):
class Bank {
name: string;
}
class BankRepository extends Repository<Bank> {
public test():void {
this.find({ name: 'foo' }); // OK!
}
}
However, If I try to make my generic, with BankModel
abstract class:
abstract class BankModel {
foo: string;
}
class BankRepository<E extends BankModel> extends Repository<E> {
test(foo: string): void {
this.find({ foo: foo }); // KO!!!
}
}
The error is:
Argument of type '{ foo: string; }' is not assignable to parameter of type 'FindConditions'.ts(2345),
The declaration of FindConditions<E>
is:
declare type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
So.. why it doesn't work?