mongoose: TS2589: Type instantiation is excessively deep and possibly infinite
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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 21 (10 by maintainers)
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.
Yes @francescov1 , and sorry i forgot to close it.
Thank you for the amazing work 💙
@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.
@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 mypopulate
return types are automatically inferred, and if they cannot be then we just fallback tomongoose.Types.ObjectId | ChildDoc
.@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 thePopulatedDocument
type entirely.I really like this
PopulatedDocument
type. Very handy if you’re defining your document interfaces usingmongoose.Types.ObjectId | ChildDoc
.PopulatedDocument
is missing a couple of edge cases. For example, it is perfectly fine to populate fields that aren’t of typeObjectId
, 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.Ok I’ll stick with this solution for now
Thank you guys for your great work, I really appreciate it
@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 https://github.com/Automattic/mongoose/pull/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.
Thanks for the ping @mohammad0-0ahmad, I’ll look into this over the weekend!
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
Hello @IslandRhythms ! I’ve got it on my setup, can you test it again after restarting VSC?
I downloaded your zip and ran it and still can’t reproduce, it’s something with your setup.