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

TS error calling findOne in a custom entity repository when there is a type circularity in the entity model #2742

Closed
itsame-luigi opened this issue Feb 11, 2022 · 0 comments

Comments

@itsame-luigi
Copy link
Contributor

Describe the bug
In my codebase I have the following:

  • An entity model that has a type circularity (see below)
  • Custom entity repositories
  • Each entity is annotated with [EntityRepositoryType]: ...

When I try to compile I get the following compile time errors:

Type instantiation is excessively deep and possibly infinite. ts(2589)
Expression produces a union type that is too complex to represent. ts(2590)
'methodName' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. ts(7023)

I haven't been able to craft a repro that produces the first two errors, but can easily produce a repro that produces the third error.

Stack trace
N/A

To Reproduce
Steps to reproduce the behavior:

  1. Attempt to compile the code in main.ts, below.
  2. Notice the error on ParticipantRepository's get method.

main.ts

class ProviderRepository extends EntityRepository<Provider> {}
class UserRepository extends EntityRepository<User> {}
class MemberRepository extends EntityRepository<Member> {}
class SessionRepository extends EntityRepository<Session> {}
class ParticipantRepository extends EntityRepository<Participant> {
  async get(session: Session, member: Member) {
    return this.findOne({ session, member });
  }
}

@Entity({ customRepository: () => ProviderRepository })
export class Provider {
  declare [EntityRepositoryType]: ProviderRepository;

  @PrimaryKey()
  id: number;

  constructor(id: number) {
    this.id = id;
  }
}

@Entity({ customRepository: () => UserRepository })
export class User {
  declare [EntityRepositoryType]: UserRepository;

  @PrimaryKey()
  id: number;

  constructor(id: number) {
    this.id = id;
  }

}

@Entity({ customRepository: () => MemberRepository })
export class Member {
  declare [EntityRepositoryType]: MemberRepository;

  @ManyToOne(() => Provider, { eager: true, primary: true })
  provider: Provider;

  @ManyToOne(() => User, { eager: true, primary: true })
  user: User;

  constructor(a: Provider, b: User) {
    this.provider = a;
    this.user = b;
  }

}

@Entity({ customRepository: () => SessionRepository })
export class Session {
  declare [EntityRepositoryType]: SessionRepository;

  @PrimaryKey()
  id: number;

  @ManyToOne(() => Member, { eager: true })
  owner: Member;

  @ManyToOne(() => Participant, { nullable: true, default: null, eager: true })
  lastActionBy: Participant | null = null;

  constructor(id: number, owner: Member) {
    this.id = id;
    this.owner = owner;
  }

}

@Entity({ customRepository: () => ParticipantRepository })
export class Participant {
  declare [EntityRepositoryType]: ParticipantRepository;

  @ManyToOne(() => Session, { eager: true, primary: true })
  session: Session;

  @ManyToOne(() => Member, { eager: true, primary: true })
  member: Member;

  constructor(session: Session, member: Member) {
    this.session = session;
    this.member = member;
  }

}

Expected behavior
No compiler errors.

Additional context

This was originally reported here: #2652 (comment).

Per #2652 (comment), modifying ExpandObject<T> to filter out functions (and symbols like EntityRepositoryType) seems to address all three errors for me.

Versions

Dependency Version
node 16.6.2
typescript 4.5.4
mikro-orm 5.0.1-dev.23
your-driver sqlite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant