Trying to implement TypeScript with TypeORM using a generic class has been challenging. The issue lies within my AbstractDal
which takes a generic parameter that is constrained to extend IEntity
. IEntity
is supposed to have an id
property, yet for some reason, the id
property is not recognized by the repository
and therefore, the findOneBy
function call does not undergo proper type checking. At this point, it's unclear whether this is a limitation of TypeScript or TypeORM.
interface IEntity {
id: number;
}
export abstract class AbstractDal<T extends IEntity> {
abstract repository: Repository<T>;
public async get(id: number): Promise<T> {
return await this.repository.findOneBy({ id });
}
}
Error:
src/abstracts/abstract.dal.ts:20:44 - error TS2345: Argument of type '{ id: number; }' is not assignable to parameter of type 'FindOptionsWhere<T> | FindOptionsWhere<T>[]'.
Types of property 'id' are incompatible.
Type 'number' is not assignable to type 'FindOptionsWhereProperty<NonNullable<T["id"]>, NonNullable<T["id"]>>'.
20 return await this.repository.findOneBy({ id });
To investigate further, refer to the Repository. To replicate the issue, simply run npm i && npm start
.