import { Repository } from 'typeorm';
export interface Entity {
id: string;
}
export class CrudService<E extends Entity> {
constructor(private readonly _repository: Repository<E>) {}
async update(id: string, dto: any) {
await this._repository.update({ id }, dto); // HERE is the issue
}
}
I get the following error:
Diagnostics:
1. Argument of type '{ id: string; }' is not assignable to parameter of type 'string | number | Date | ObjectId | string[] | number[] | Date[] | ObjectId[] | FindOptionsWhere<E>'.
Types of property 'id' are incompatible.
Type 'string' is not assignable to type 'Uint8Array | FindOptionsWhereProperty<NonNullable<E["id"]>, NonNullable<E["id"]>>'. [2345]
I want the understand the problem why Typescript is throwing error about the id field, because when I substitute the Generic with the Entity everything works fine. I do need to use the FindOneOptions interface for that operation, I would like to avoid queryBuilder or findById or any other workarounds with as type-casting.
import { Repository } from 'typeorm';
export interface Entity {
id: string;
}
export class CrudService { // Removed Generic
constructor(private readonly _repository: Repository<Entity>) {} // Changed E to Entity
async update(id: string, dto: any) {
await this._repository.update({ id }, dto); // Works fine
}
}
I really want to solve this