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

FindOptionsRelations when @OneToMany #9350

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

FindOptionsRelations when @OneToMany #9350

Stephen2 opened this issue Sep 7, 2022 · 2 comments

Comments

@Stephen2
Copy link

Stephen2 commented Sep 7, 2022

Issue Description

Have an Entity

User {
  @OneToMany(() => Child, ({ parent }) => parent, { cascade: true })
  children!: readonly Child[] | undefined;
}

Have an Entity

Child {
  @ManyToOne(() => User, ({ children }) => children, { nullable: false, onDelete: "CASCADE" })
  parent!: User | undefined;

  @OneToMany(() => Claim, ({ child }) => child, { cascade: true })
  claims!: readonly Claim[] | undefined;
}

Try and select user+children+claims:

    const user = await baseRepository.findOne({
      relations: {
        children: {
          claims: true,
          journalEntries: true,
        },
      },
      where: {
        id,
      },
    });

Get compile time error:

Type '{ children: { claims: true; journalEntries: true; }; }' is not assignable to type 'FindOptionsRelationByString | FindOptionsRelations<User>'.
  Types of property 'children' are incompatible.
    Type '{ claims: true; journalEntries: true; }' is not assignable to type 'boolean | readonly (boolean | FindOptionsRelations<Child> | undefined)[] | undefined'.
      Object literal may only specify known properties, and 'claims' does not exist in type 'readonly (boolean | FindOptionsRelations<Child> | undefined)[]'.ts(2322)

Such that you have to write code:

      relations: {
        children: [
          {
            claims: true,
            journalEntries: true,
          },
        ],
      },

Note the addition of [] which doesn't make sense when writing code.

Causes runtime error: EntityPropertyNotFoundError: Property "0" was not found in "Child". Make sure your query is correct.

Can workaround with ts-expect-error:

      relations: {
        children: {
          // @ts-expect-error TypeORM types want this to be an [] becuase it's silly
          // That causes runtime error: EntityPropertyNotFoundError: Property "0" was not found in "Child". Make sure your query is correct.
          claims: true,
          journalEntries: true,
        },
      },

Expected Behavior

FindOptionsRelations types to be modified for this nested case

Actual Behavior

Types are wrong, error messages shown above

Steps to Reproduce

As above

My Environment

Dependency Version
Operating System
Node.js version 16.15.1
Typescript version 4.8.2
TypeORM version 0.3.9

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

  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@iJhefe
Copy link
Contributor

iJhefe commented Oct 5, 2022

it's occurs because your property type is "readonly" and the FindOptionsRelationsProperty interface does not handle this.
May it be fixed just adding to FindOptionsRelationsProperty

Property extends Readonly<Array<infer I>> ? FindOptionsRelationsProperty<NonNullable<I>> | boolean

@Stephen2
Copy link
Author

Stephen2 commented Oct 5, 2022

Awesome thank you for the find @iJhefe !

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

2 participants