TypeORM loadRelationCountAndMap doesn't infer anything to the response (Typescript)

Viewed 20

If my entities look like this:

@Entity({ name: 'libraries' })
export class Library {
  @PrimaryGeneratedColumn('uuid')
  id: string;
}

@Entity({ name: 'books' })
export class Book {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ManyToOne(() => Library, (lib) => lib.books)
  @JoinColumn({ name: 'library_id' })
  library: Library;
}

And my Library Dto looks like this:

export class LibraryDto {
  @IsUUID()
  id: string;

  @IsNumber()
  bookCount: number;

How can I make this function work with typescript?

  async findLibraryById(libraryId: string): Promise<LibraryDto>

I'm using .loadRelationCountAndMap('library.bookCount', 'library.books') to get the books count, but that doesn't infer any type difference and libraryRepository.createQueryBuilder() return type is always a Library regardless of select, loadRelationCountAndMap or whatever, how can I fix this?

0 Answers
Related