#Typeorm Update typing issue

2 messages · Page 1 of 1 (latest)

native swan
#
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
inland rover
#

I guess this might be a case of accidental interface augmentation since typeorm also has an interface named Entity