mongoose: 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? It seems if a model defintion has a property with type “any”, all calls to findOne etc have typescript errors with error TS2589: Type instantiation is excessively deep and possibly infinite.

If the current behavior is a bug, please provide the steps to reproduce.

interface IDBModel extends Document { 
        date: Date; // date created
	_tags: any[];
}

const Schema = new Schema({
date: { type: Date, default: Date.now }, // date created
_tags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }]
});

const DBModel: Model<IDBModel> = model<IDBModel>('Meep', Schema);

DBModel.findOne({date: new Date()})... // error

What is the expected behavior? should not throw the TS error.

As a workaround, setting it to “unknown” works

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. mongoose 6.0.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 33 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I also get this error after updating from mongoose@5.13.7 to mongoose@5.13.8. It also occurs when updating to 6.0.1. @types/node is LTS, the rest of the packages are up to date.

It occurs when you have an array of references:

const itemTypeSchema = new Schema<IItemType, IItemTypeModel>({
  // removed other attributes that cause no error here
  attributeGroups: [{ // this causes the error
    type: Types.ObjectId,
    required: true,
    ref: 'AttributeGroup',
  }],
});

The error occurs only when extending Document for IItemType. If not extending it, there is no _id property, which makes filtering an other operations impossible.

Also I tried and failed migration to version 6. There is documentation missing that explains how to correctly use PopulatedDoc with Typescript in complex situations.

Only workaround seems to be using strings instead of Types.ObjectId.

@rongem we backported some fixes to 5.x that look like they fix your issue. These will be released in 5.3.12.

Is there anything preventing you from upgrading to Mongoose 6? These issues are fixed in Mongoose 6.0.10.

@vkarpov15 Sorry for the late response…have been putting out fires for the last couple of weeks…it looks like you now have an example of this problem thanks to @rongem , correct?

If so, would it be possible to include this update in a 5.13.10 patch? We are are currently not in a position to update to the 6.x version of the library.

Thanks so much for helping with this issue!

Kelly