-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Type instantiation is excessively deep and possibly infinite. #10617
Comments
@simllll below script compiles fine on my machine with Mongoose 6.0.0 and 6.0.1, no errors. Can you please clarify your TypeScript version and tsconfig, and modify the below script to demonstrate your issue? import { connect, model, Schema, Model } from 'mongoose';
run().catch(err => console.log(err));
async function run() {
await connect('mongodb://localhost:27017/test');
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()})
} |
i get this error when i try to use SomeModel.find({}) |
@LOLBRUHNICE please provide a repro script or at least some code samples |
I also get this error after updating from [email protected] to [email protected]. It also occurs when updating to 6.0.1. It occurs when you have an array of references:
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. |
interface XXX extends mongoose.Document {
SomeProperty: any[]
} |
it happen once i upgrade to 6.x.x |
@vkarpov15 I tried to create a reproducable example, and stumpled upon something very strange. import { Document, Model, model, Schema } from 'mongoose';
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' }]
});
export const DBModel: Model<IDBModel> = model<IDBModel>('Meep', schema);
run().catch(err => console.log(err));
async function run() {
await DBModel.findOne({
[`internal.trackingPingBacks.action`]: 'asdf'
});
} is basically the same as yours, just minor modifications. Not quite sure if this is a typescript bug, or what exactly is going on here. Do you have any idea or should I open a ticket at typescript? tsc --version: 4.3.5 |
I found that we can replace |
@simllll I took a closer look and this looks to be caused by our One way you can fix this issue is to remove the |
@vkarpov15: This hint does not solve the problem, but moves it to another place. What is missing is documentation that explains how to use sub documents and references in TypeScript without using the extends document. Imagine an array of subdocuments in a document and then using a query filter looking for special ObjectIds inside the sub documents. |
@rongem can you please provide code samples that illustrate the issue you're seeing? What sort of special ObjectIds? Also, if you upgrade to v6.0.4 this compilation issue should be fixed, even if you're using |
@rongem still no luck, below script compiles fine on both Mongoose 6.0.9 and Mongoose 6.0.1 import { Document, Model, model, Schema, Types } from 'mongoose';
export interface IAttributeGroup extends Document {
name: string;
}
const attributeGroupSchema = new Schema<IAttributeGroup, IAttributeGroupModel, IAttributeGroup>({
name: {
type: String,
required: true,
unique: true,
},
});
attributeGroupSchema.statics.validateIdExists = (value: Types.ObjectId) => attributeGroupModel.findById(value).countDocuments()
.then((docs: number) => Promise.resolve(docs > 0))
.catch((error: any) => Promise.reject(error));
attributeGroupSchema.pre('find', function() { this.sort('name'); });
export interface IAttributeGroupModel extends Model<IAttributeGroup> {
validateIdExists(value: Types.ObjectId): Promise<boolean>;
}
export const attributeGroupModel = model<IAttributeGroup, IAttributeGroupModel>('AttributeGroup', attributeGroupSchema);
export async function itemTypeModelCreate(name: string) {
let itemType = await attributeGroupModel.create({ name });
} Can you please create a TypeScript playground example that demonstrates the issue you're seeing @skhilliard or @rongem ? I haven't been able to repro this issue. |
@vkarpov15: I am an old man and no longer seen on playgrounds. :) If you want the full example (with more errors of that kind), you may use the njs-backend project on https://github.com/rongem/cmdb |
…inition to avoid "excessively deep and possibly infinite" TS errors Fix #10617
@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: I tried to port it already, but failed. The migration documentation seems incomplete. Handling of strings where BSON is used in MongoDB seems to be different. |
"Handling of strings where BSON is used in MongoDB seems to be different." <-- any more info on this? |
In version 5, there was no problem using strings for object definitions when using ObjectIds in schema. Mongoose did all the heavy lifting. I created a branch where you can see the problem: https://github.com/rongem/cmdb/tree/mongoose6 If you open folder njs-backend in VS Code and then navigate to /src/models/mongoose/historic-connection.model.ts, you'll see the mess after the upgrade. I have no idea in which direction I will have to fix this, and there is no mongoose/typescript documentation that helps. What is there ends a few millimeters below the surface. |
@vkarpov15: I can confirm that the initial error "Type instantiation is excessively deep and possibly infinite" is resolved in 5.13.12. Thank you for that. Nevertheless, all the new type restrictions from Mongoose 6 now also apply, so it seems I have to fix them anyway. Sigh.
|
To make the newly introduced problems clearer: After fixing all breaking changes to v 5, I made up a new branch for mongoose6: https://github.com/rongem/cmdb/tree/mongoose6. |
@vkarpov15 After running trough the migration guide and changing everything that affects me, I still have type errors. To make clear where the documentation has flaws, I show you two examples. The documentation under https://mongoosejs.com/docs/typescript/schemas.html only contains text fields, no SubDocuments nor ObjectIds. I tried two variants, and both throw compilation errors of the same type when changing vom v5 to v6: 1.) Working with PopulatedDoc
2.) Working with strings only
Oh, and by the way: using Types.ObjectId instead of string throws the same error. I have absolutely no idea how to correctly design the schema as long as the documentation keeps silent on that. |
@rongem I agree and have a lot of struggles with the lack of documentation for Typescript. One thing though, I have had some success using Hope that helps. |
@rongem that's because you're defining your schema incorrectly. Like @mjfwebb said, replace: const historicConnectionSchema = new Schema<IHistoricConnection, Model<IHistoricConnection>>({
connectionRuleId: {
type: Types.ObjectId,
required: true,
},
// ...
}) with: const historicConnectionSchema = new Schema<IHistoricConnection, Model<IHistoricConnection>>({
connectionRuleId: {
type: Schema.Types.ObjectId, // <-- ObjectId SchemaType, **not** the actual ObjectId constructor
required: true,
},
// ...
}) Type errors are expected in this case. |
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.
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
The text was updated successfully, but these errors were encountered: