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

TS2589: Type instantiation is excessively deep and possibly infinite #11787

Closed
HamoBoker opened this issue May 9, 2022 · 21 comments
Closed

TS2589: Type instantiation is excessively deep and possibly infinite #11787

HamoBoker opened this issue May 9, 2022 · 21 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@HamoBoker
Copy link

HamoBoker commented May 9, 2022

Do you want to request a feature or report a bug?
bug

What is the current behavior?
i'm generating interfaces using mongoose-tsgen and everything works fine in mongoose <=6.3.1, but on v6.3.2 or v6.3.3 i get this ts error TS2589: Type instantiation is excessively deep and possibly infinite, also im not using @types/mongoose

If the current behavior is a bug, please provide the steps to reproduce.
await SomeModel.findById(id)
you can test example code in mongoose-tsgen

tsconfig.json

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "moduleResolution": "Node",
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "removeComments": true,
    "noUnusedLocals": false,
    "newLine": "lf",
    "noImplicitAny": true,
    "pretty": true,
    "noEmitOnError": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules/**"]
}

What is the expected behavior?
ts build without errors

Thank you so much for you great work

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels May 11, 2022
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented May 11, 2022

import * as mongoose from 'mongoose';
import { UserDocument, UserModel, UserSchema, UserObject} from './src/interfaces/mongoose.gen'

const UserSchema: UserSchema = new mongoose.Schema({
    name: String
});

const User = mongoose.model<UserDocument, UserModel>('User', UserSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();
    const entry = await User.create({ name: 'Test'});
    await User.findById(entry._id);
    console.log('done');
}

run();

tsconfig

{
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "moduleResolution": "Node",
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "removeComments": true,
    "noUnusedLocals": false,
    "newLine": "lf",
    "noImplicitAny": true,
    "pretty": true,
    "noEmitOnError": true
  },
  "exclude": ["node_modules/**"]
}

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 11, 2022
@HamoBoker
Copy link
Author

@IslandRhythms thanks for response, I created a new project and i used your snippet, and still get the same error but on schema only

image

same code in above comment

import * as mongoose from 'mongoose';
import { UserDocument, UserModel, UserSchema, UserObject } from '../interfaces/mongoose.gen'

const UserSchema: UserSchema = new mongoose.Schema({
  name: String
});

const User = mongoose.model<UserDocument, UserModel>('User', UserSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  const entry = await User.create({name: 'Test'});
  await User.findById(entry._id);
  console.log('done');
}

run();

package.json

  "dependencies": {
    "mongoose": "^6.3.3"
  },
  "devDependencies": {
    "mongoose-tsgen": "^9.0.5",
    "nodemon": "^2.0.16",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.4"
  }

same tsconfig

{
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "moduleResolution": "Node",
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "removeComments": true,
    "noUnusedLocals": false,
    "newLine": "lf",
    "noImplicitAny": true,
    "pretty": true,
    "noEmitOnError": true
  },
  "exclude": ["node_modules/**"]
}

@IslandRhythms
Copy link
Collaborator

It must be something with your setup, I can't reproduce it on my end.

@Uzlopak
Copy link
Collaborator

Uzlopak commented May 11, 2022

Does it also happen when transpiling with tsc? Does it only happen in vscode? Which typescript version do you use in vscode? Which in your project? What happens when you update typescript? What when you update vscode?

@HamoBoker
Copy link
Author

HamoBoker commented May 11, 2022

Yes, i'm using webstorm but i tested it on vscode also i get same error, on mongoose@6.3.1 everything works fine but its not working on 6.3.2 & 6.3.3

node: v16.15.0
typescript: v4.6.4 (latest) in project & IDE & globally
device: macOS 12.3.1 (M1)

when running tsc command i get this

src/models/User.ts:4:52 - error TS2589: Type instantiation is excessively deep and possibly infinite.

4 const UserSchema: UserSchema = new mongoose.Schema({
                                                     ~
5   name: String
  ~~~~~~~~~~~~~~
6 });
  ~

heres the new project that contains the code snippet above and it produces the problem
node-demo.zip

npm i && npx mtgen

@mohammad0-0ahmad
Copy link
Contributor

mohammad0-0ahmad commented May 12, 2022

By using mongoose v6.3.1 the error will disappear for now.
I guess LeanDocument type "not sure yet" cause this breaking change.
I think @taxilian can help with that.

@IslandRhythms
Copy link
Collaborator

I downloaded your zip and ran it and still can't reproduce, it's something with your setup.

@mohammad0-0ahmad
Copy link
Contributor

mohammad0-0ahmad commented May 13, 2022

I downloaded your zip and ran it and still can't reproduce, it's something with your setup.

Hello @IslandRhythms !
I've got it on my setup, can you test it again after restarting VSC?

Untitled

@IslandRhythms
Copy link
Collaborator

Got it

@IslandRhythms IslandRhythms added typescript Types or Types-test related issue / Pull Request and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels May 13, 2022
@mohammad0-0ahmad
Copy link
Contributor

mohammad0-0ahmad commented May 23, 2022

Hello @HamoBoker, @jainil, @francescov1!
I've noticed that populate FN overloading in auto generated type "mongoose.gen" is what cause this error with the latest versions.
So you might check that to solve this issue.
I don't think we need to fix this breaking change in mongoose. @IslandRhythms

@francescov1
Copy link
Contributor

Thanks for the ping @mohammad0-0ahmad, I'll look into this over the weekend!

@francescov1
Copy link
Contributor

@HamoBoker This incompatibility is turning out to be a bit tougher to uncover than I expected. For now, you can continue using mongoose-tsgen with the --no-populate-overload flag to resolve the problem (but won't be able to benefit from population inference).

@mohammad0-0ahmad Any idea on the expected timeline for your PR #11563. This PR removes the need for mongoose-tsgen, so depending on when it gets merged it may not be worth trying to fix the issue in mongoose-tsgen.

@mohammad0-0ahmad
Copy link
Contributor

@francescov1 sorry for that, I don't really know when it will be exactly merged, but @vkarpov15 said it will be shipped with the next minor release "6.4.0"

@HamoBoker
Copy link
Author

Ok I'll stick with this solution for now

Thank you guys for your great work, I really appreciate it

@vkarpov15
Copy link
Collaborator

@francescov1 thanks for looking into this and thanks for the lib, mongoose-tsgen is 👍 . You're right that the populate() override is what causes the "Type instantiation is excessively deep and possibly infinite" error, however, I haven't been able to figure out a way to avoid that error without removing the PopulatedDocument type entirely.

I really like this PopulatedDocument type. Very handy if you're defining your document interfaces using mongoose.Types.ObjectId | ChildDoc. PopulatedDocument is missing a couple of edge cases. For example, it is perfectly fine to populate fields that aren't of type ObjectId, like numbers or strings. We even support populating subdocuments in place.

But, again, this sort of TypeScript bug is very hard to diagnose. We'll keep this open for the future, and consider pulling in the PopulatedDocument abstraction into Mongoose's types.

@vkarpov15 vkarpov15 modified the milestones: 6.3.7, 6.3.8 Jun 11, 2022
@francescov1
Copy link
Contributor

francescov1 commented Jun 12, 2022

@vkarpov15 Thanks for checking this out, too bad there are not any obvious fixes.

Sounds like the PopulatedDocument type may still be useful; I'd be happy to help integrate it into Mongoose if this is the case, let me know! I find it to be quite nice since most of my populate return types are automatically inferred, and if they cannot be then we just fallback to mongoose.Types.ObjectId | ChildDoc.

@vkarpov15 vkarpov15 modified the milestones: 6.3.9, 6.x Unprioritized Jun 17, 2022
@francescov1
Copy link
Contributor

@vkarpov15 Can we expect this to be resolved by the Mongoose team, or is this is an issue with mongoose-tsgen? Would like to try getting it resolved soon since its a breaking issue for many users. I havent had any luck fixing it yet, but not sure if I should be focusing on fixing it from my end or from the Mongoose types. Also just finding it hard to track down the root cause since the error is so generic.

@mohammad0-0ahmad
Copy link
Contributor

@francescov1 try to override LeanDocument helper type, I think this issue is related to the changes made on that.

@vkarpov15
Copy link
Collaborator

@francescov1 as far as I can tell, this is an issue with mongoose-tsgen. We've been trying to improve the mongoose-tsgen type output, but haven't been able to fix it.

@francescov1
Copy link
Contributor

I just reviewed this again, it seems the issue has been resolved as of Mongoose v6.4.1 (so it only affects Mongoose 6.3.2 - 6.4.0). I think we can close this @HamoBoker @vkarpov15 ?

@HamoBoker
Copy link
Author

Yes @francescov1 , and sorry i forgot to close it.

Thank you for the amazing work 💙

@vkarpov15 vkarpov15 removed this from the 7.x Unprioritized milestone Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

6 participants