Trying to create a transaction based entity from base entity provided by typeORM

Viewed 116

I am trying to extend BaseEntity provided by TypeORM and create a transaction based entity that I can later use to create all my entities.

import "reflect-metadata";
import { Transaction } from "server/common/transactions";
import { BaseEntity, ObjectType, SelectQueryBuilder } from "typeorm";

export class TransactionalBaseEntity extends BaseEntity {
  hasId(): boolean {
    const transactionManager = Transaction.instance.getTransactionManagerOrManager();
    return transactionManager.hasId(this);
  }
  static createQueryBuilder<T extends TransactionalBaseEntity>(
    this: ObjectType<T>,
    alias?: string
  ): SelectQueryBuilder<T> {
    const transactionManager = Transaction.instance.getTransactionManagerOrManager();
    return transactionManager.getRepository(this).createQueryBuilder(alias);
  }
}

Doing so my IDE(vs-code) is showing the following error -

Class static side 'typeof TransactionalBaseEntity' incorrectly extends base class static side 'typeof BaseEntity'.
  The types returned by 'createQueryBuilder(...).getRawAndEntities()' are incompatible between these types.
    Type 'Promise<{ entities: BaseEntity[]; raw: any[]; }>' is not assignable to type 'Promise<{ entities: T[]; raw: any[]; }>'.
      Type '{ entities: BaseEntity[]; raw: any[]; }' is not assignable to type '{ entities: T[]; raw: any[]; }'.
        Types of property 'entities' are incompatible.
          Type 'BaseEntity[]' is not assignable to type 'T[]'.
            Type 'BaseEntity' is not assignable to type 'T'.
              'BaseEntity' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'BaseEntity'.ts(2417)

Can't seem to wrap my head around what I am doing wrong. This issue comes only when I add the createQueryBuilder method. The hasId method doesn't give the error.

0 Answers
Related