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

TS2321: Excessive stack depth comparing types 'MongoRepository<Entity>' and 'Repository<Entity>' #3194

Closed
ghost opened this issue Dec 4, 2018 · 33 comments

Comments

@ghost
Copy link

ghost commented Dec 4, 2018

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[x] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:
I am trying to build an API with typeorm and type-graphql. The following code should work, but it gives me a strange error: [at-loader] TS2321: Excessive stack depth comparing types 'MongoRepository<Entity>' and 'Repository<Entity>'.

import { Resolver, Mutation, Query, Arg, Ctx } from "type-graphql";
import { MongoRepository } from "typeorm";
import { InjectRepository } from "typeorm-typedi-extensions";
import { User } from "../entities/user";

@Resolver(of => User)
export class UserResolver {
  constructor(
    @InjectRepository(User) private readonly userRepository: MongoRepository<User>
  ) {}

  @Query(returns => User, { nullable: true })
  userBySlug(@Arg("slug") slug: string) {
    return this.userRepository.findOne({slug});
  }
}

It works just fine, if I use Repository instead of MongoRepository, and I've found a way to circumvent this by calling createEntityCursor(query), but I want to use projections in my queries. The projection object cannot be passed to createEntityCursor(), and find() & findOne() methods raise that TS2321 error. I've also found this typescript issue, but I am unsure what to do next. Is there a way to get pass this error?

@pleerock
Copy link
Member

pleerock commented Jan 8, 2019

Have no idea. @Dverlik did you figure this out?

@ghost
Copy link
Author

ghost commented Jan 11, 2019

@pleerock Well, I injected both MongoRepository and Repository. The solution is shitty, but it works.

At some other projects I gave up and moved to MySQL.

Some people said adding require('lodash') helped them, though I didn't try to resolve it like that.

@pleerock
Copy link
Member

Im sorry about your experience but glad that you moved to relational database haha. I'll close it, if somebody reproduce this error please provide some example.

@ThisIsDjonathan
Copy link

ThisIsDjonathan commented Jan 13, 2019

Same Excessive stack depth comparing types 'T' and 'DeepPartial' error with MySQL here.

 public async create(obj: T): Promise<T> {
    const connection = await DatabaseProvider.getConnection();
    return await connection.getRepository(this.entity).save(obj);
  }

@pleerock
Copy link
Member

@oDjonathanKrause please provide some reproduction

@ThisIsDjonathan
Copy link

ThisIsDjonathan commented Jan 14, 2019

@pleerock

Database system/driver:
[x ] mssql

TypeORM version:
[x] latest

Steps to reproduce the error:
I was trying to build a Service class to generalize some methods that access the database.

But if the class model its passed by param using it to get the repository and save, The error it's throwed "Excessive stack depth comparing types 'T' and 'DeepPartial'. [2321]".

The workaround was to create an instance of the Model on the constructor the generic class, clone it and pass the attributes of param object to the cloned object before save.

export class Service<T>  {
  instanceOfEntity: any;
  constructor(private entity: { new(): T }) { 
    this.instanceOfEntity = new entity;
  }
  
  // Doing by this way I had some problems on save.
  public async create(obj: T): Promise<T> {
    const connection = await DatabaseProvider.getConnection();
    return await connection.getRepository(this.entity).save(obj);
  }

 // workaround
 public async create(obj: T): Promise<T> {
    let newObj = this.clone(this.instanceOfEntity);
    for(let property in obj) {
      newObj[property] = obj[property];
    } 

    const connection = await DatabaseProvider.getConnection();
    return await connection.getRepository(this.entity).save(newObj);
  }

  public clone<T>(instance: T): T {
    const clone = new (instance.constructor as { new (): T })();
    Object.assign(clone, instance);
    return clone;
  }
}

@ancyrweb
Copy link

ancyrweb commented Mar 15, 2019

this solved it for me !

@MSafter
Copy link

MSafter commented Jun 14, 2019

Updating ts-node to 8.2.0 solved it for me!

@jeremy-ww
Copy link

Skip this problem temporarily by setting skipLibCheck: true in tsconfig.json.

@eugenchio
Copy link

The error occurs for me after updating to newest release of TypeScript@3.6.2. With TypeScript@3.5.3 everything works fine.
@Army-U , thanks your suggestion works. But it doesn't seem to be a good solution :(

@bennycode
Copy link

Same error (TS2321) shows up in my application with TypeScript 3.6.2 while using @nestjs/core which supports TypeORM. Seems like there is something going on: nestjs/nest#2834

@guillaume-roy
Copy link

We've just downgraded to typescript@3.5.3 to solve the issue.
See : microsoft/TypeScript#29112

@rubiin
Copy link

rubiin commented Aug 30, 2019

Seems like an issue with the latest typescript version, for the time being decided to stick to typescript 3.5.3

@hcharley
Copy link

Also seeing this, and will stick to typescript@3.5.3

@davidquintard
Copy link

i'm using typescript@3.5.3 but i still get this issue.

C:\Data\Projects\nestjs (master -> origin)
λ npm run build

> pairmix@0.0.1 build C:\Data\Projects\nestjs
> tsc -p tsconfig.build.json

error TS2321: Excessive stack depth comparing types 'MongoRepository<Entity>' and 'Repository<Entity>'.

node_modules/typeorm/repository/MongoRepository.d.ts:12:22 - error TS2589: Type instantiation is excessively deep and possibly infinite.

12 export declare class MongoRepository<Entity extends ObjectLiteral> extends Repository<Entity> {
                        ~~~~~~~~~~~~~~~


Found 2 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! pairmix@0.0.1 build: `tsc -p tsconfig.build.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the pairmix@0.0.1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@davidquintard
Copy link

I upgraded to latest versions, error is more precise:
"@nestjs/typeorm": "^6.1.3",
"typescript": "3.6.2",

> pairmix@0.0.1 build C:\Data\Projects\nestjs
> tsc -p tsconfig.build.json

error TS2321: Excessive stack depth comparing types 'MongoRepository<Entity>' and 'Repository<Entity>'.

node_modules/typeorm/repository/MongoRepository.d.ts:12:22 - error TS2589: Type instantiation is excessively deep and possibly infinite.

12 export declare class MongoRepository<Entity extends ObjectLiteral> extends Repository<Entity> {
                        ~~~~~~~~~~~~~~~

node_modules/typeorm/repository/MongoRepository.d.ts:45:5 - error TS2321: Excessive stack depth comparing types '(optionsOrConditions?: string | number | Date | ObjectID | FindOneOptions<Entity> | Partial<Entity>, maybeOptions?: FindOneOptions<Entity>) => Promise<Entity>' and '{ (id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity>; (options?: FindOneOptions<Entity>): Promise<Entity>; (conditions?: FindConditions<Entity>, options?: FindOneOptions<...>): Promise<...>; }'.

45     findOne(optionsOrConditions?: string | number | Date | ObjectID | FindOneOptions<Entity> | Partial<Entity>, maybeOptions?: FindOneOptions<Entity>): Promise<Entity | undefined>;
       ~~~~~~~

@lukejagodzinski
Copy link
Contributor

I have the same error. The weird thing is that it shows MongoRepository error and I'm not even using MongoDB... I'm using Postgres. Moreover, the weird thing is that it doesn't happen all the time. It started happening after I moved into mono repository. My package that I'm trying to build consists only of the model definitions and migrations. Nothing else. For now I had to use the skipLibCheck option.

@sgronblo
Copy link

sgronblo commented Sep 3, 2019

I'm trying to use FoalTS which includes typeorm and immediately encountered the same MongoRepository "Excessive stack depth" issue as well.

@MichaelHindley
Copy link

Same error on 3.6 when using repository.find() with postgres, reverting to 3.5 fixes it.

@bennycode
Copy link

@pleerock can you reopen this ticket?

@eropple
Copy link

eropple commented Sep 8, 2019

FWIW, this error does not occur when using typeorm@next.

It does occur regardless of version of TypeScript, having tested multiple releases of 3.4.x, 3.5.x, 3.6.x, all the way to 3.7.0-dev.

@bennycode
Copy link

FWIW, this error does not occur when using typeorm@next.

I can confirm that the issue seems to be fixed in typeorm v0.3.0-alpha.23 :)

@lookfirst
Copy link

lookfirst commented Sep 10, 2019

v0.3.0-alpha.23 aka next, is currently This branch is 81 commits ahead, 346 commits behind master.. Any way to get this to work without having to resort to @ next?

Ah, a more palatable solution (for me) is to use typescript@next, which seems to have resolved the issue microsoft/TypeScript#33144

@alexmantaut
Copy link

@lookfirst does Typescript 3.6.3 fix the issue for you?

@lookfirst

This comment has been minimized.

@fullstackwebdev
Copy link

fullstackwebdev commented Sep 29, 2019

@alexmantaut
typescript 3.6.3 did not fix the issue for me

@netpoe
Copy link

netpoe commented Mar 22, 2020

@alexmantaut
typescript 3.6.3 did not fix the issue for me

Me neither, I'm at ^3.8.3

@imnotjames
Copy link
Contributor

I cannot replicate this.

Can someone create a repository that replicates this problem?

@peisenmann
Copy link

Related to microsoft/TypeScript#34933

@imnotjames
Copy link
Contributor

When opening an issue, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This boils down to ensuring your code that reproduces the problem follows the following guidelines:

  • Minimal – Use as little code as possible that still produces the same problem
  • Complete – Provide all parts someone else needs to reproduce your problem in the question itself
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

Minimal

The more code there is to go through, the less likely people can find your problem. Streamline your example in one of two ways:

  1. Restart from scratch. Create a new program, adding in only what is needed to see the problem. Use simple, descriptive names for functions and variables – don’t copy the names you’re using in your existing code.
  2. Divide and conquer. If you’re not sure what the source of the problem is, start removing code a bit at a time until the problem disappears – then add the last part back.

Don't sacrifice clarity for brevity when creating a minimal example. Use consistent naming and indentation, and include code comments if needed. Use your code editor’s shortcut for formatting code.

Don't include any passwords or credentials that must be kept secret.

Complete

Make sure all information necessary to reproduce the problem is included in the issue itself.

If the problem requires some code as well as some XML-based configuration, include code for both. The problem might not be in the code that you think it is in.

Use individual code blocks for each file or snippet you include. Provide a description for the purpose of each block.

DO NOT use images of code. Copy the actual text from your code editor, paste it into the issus, then format it as code. This helps others more easily read and test your code.

Reproducible

To help you solve your problem, others will need to verify that it exists.

Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question.

Eliminate any issues that aren't relevant to the problem. If your question isn’t about a compiler error, ensure that there are no compile-time errors.

Double-check that your example reproduces the problem! If you inadvertently fixed the problem while composing the example but didn't test it again, you'd want to know that before asking someone else to help.

@imnotjames
Copy link
Contributor

Pretty sure this is a typescript issue, though, which was linked. If you believe it isn't, please create a new issue with a reproducible example.

@S-a-k-s-h-i
Copy link

S-a-k-s-h-i commented Sep 15, 2021

Hello everyone, getting this error :

  • error TS2321: Excessive stack depth comparing types 'any' and 'FindConditions'. return await this.entity.find(relations);
    This is the code that is giving error
    public async findWithRelations(relations: any): Promise<T[]> {
    return await this.entity.find(relations);
    }

@almandoro
Copy link

For those, that experience the issue in VSCode and eslint / tsc works fine:
make sure to choose the workspace version of TS

CMD + P : >Select typescript Version

image

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