Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadRelationCountAndMap doesn't infer (Typescript) #9353

Open
alielkhateeb opened this issue Sep 7, 2022 · 2 comments
Open

loadRelationCountAndMap doesn't infer (Typescript) #9353

alielkhateeb opened this issue Sep 7, 2022 · 2 comments

Comments

@alielkhateeb
Copy link

Issue Description

If my entities look like this:

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

  @OneToMany(() => Book, (book) => book.library)
  books: Book[];
}

@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>

Running code like this:

    const library = await this.librariesRepository
      .createQueryBuilder('library')
      .loadRelationCountAndMap('library.bookCount', 'library.books')
      .where({ id })
      .getOne();

    console.log(library.bookCount); // This will not work, Typescript complains that bookCount doesn't exist on Library

Expected Behavior

I expect I'm able to reach the property I selected using loadRelationCountAndMap

Actual Behavior

There is absolutely no way I found to bypass the typescript issue:
Property 'bookCount' does not exist on type 'Library'

My Environment

Dependency Version
Operating System
Node.js version 16.17.0
Typescript version 4.6.4
TypeORM version 0.3.9

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql I don't know
aurora-postgres I don't know
better-sqlite3 I don't know
cockroachdb I don't know
cordova I don't know
expo I don't know
mongodb I don't know
mysql I don't know
nativescript I don't know
oracle I don't know
postgres YES
react-native I don't know
sap I don't know
spanner I don't know
sqlite I don't know
sqlite-abstract I don't know
sqljs I don't know
sqlserver I don't know

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@denysovvl
Copy link

+1

1 similar comment
@NicolasPotierA
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants