TypeORM find/where does not work complex types

45 views Asked by At

I have the following entityt

@Entity('address')
export class Address {
  @IdProperty()
  @Required()
  id: AddressId;
  
  ...
}

AddressId is

export class AddressId extends Id {
  public static PREFIX = 'AD';

  public getPrefix(): string {
    return AddressId.PREFIX;
  }
}

Where @IdProperty() is

export function IdProperty() {
  return useDecorators(
    Property(String),
    OnDeserialize((id) => {
      if (id) {
        return // function to convert to AddressId
      }
      return null;
    }),
    OnSerialize((id) => {
      if (id) {
        return id.getValue();
      }

      return null;
    }),
  );
}

Now when I try to call any find/where functions, for example

 async findById(id: AddressId): Promise<Address | null> {
    return this.findOneBy({ id });
  }

I have the following error:

TS2322: Type  AddressId  is not assignable to type
boolean | FindOperator<any> | FindOptionsWhere<AddressId> | FindOptionsWhere<AddressId>[] | EqualOperator<AddressId> | undefined

  Type  AddressId  is not assignable to type  FindOptionsWhere<AddressId> 
    Types of property  getPrefix  are incompatible.
      Type  () => string  is not assignable to type  undefined 

Any ideas why and what to do to fix it? If I @ts-ignore, the function correctly works - it's just the type that is invalid

0

There are 0 answers