-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Typescript: Type instantiation is excessively deep and possibly infinite #10189
Comments
|
@IslandRhythms Do the labels mean you cannot reproduce the problem and want me to try out your script? Or that you have been able to reproduce it but didn't remove the previous "can't reproduce" label? |
I couldn't reproduce it. You can run the script and see if the problem reproduces for you, make adjustments to show how to get the bug to pop up. If you run this script and get the error, that means you have a problem with your setup |
@mjfwebb where does |
@vkarpov15 Note the breadcrumbs at the top of this picture I'm going to try with the repro script now. |
I've managed to reproduce the problem in a new project. I have a CodeSandbox example. but the code will need to be run elsewhere for the error to show (since CodeSandbox can't use mongoose.connect, or I am using the App incorrectly): https://codesandbox.io/s/infinte-depth-example-4dw9r If needed I can upload the test project somewhere. |
Thanks for the repro script, we've confirmed that this is an issue and we found a couple of workarounds. The funny thing is that, if you remove 'fourth', everything works fine, so it isn't an infinite type recursion, just that we're hitting an arbitrary limit in TypeScript. Specifically, this error is caused by Mongoose's However, we've managed to find a couple of workarounds. One workaround we've managed to find is to not use import { Schema, Document, model } from "mongoose";
const FourthSchema = new Schema({
name: { type: String, required: true }
});
export interface IFourthSchema { // <-- remove `extends Document`
name: string;
}
const Fourth = model<IFourthSchema>("Fourth", FourthSchema);
export default Fourth; Based on our experience working with TypeScript so far, we recommend not using Another workaround is to use import { Schema, Document, model, PopulatedDoc } from "mongoose";
import { IFourthSchema } from "./fourth";
const ThirdSchema = new Schema({
name: { type: String, required: true },
fourthSchemaObject: {
type: Schema.Types.ObjectId,
ref: "Fourth"
}
});
export interface IThirdSchema extends Document {
name: string;
fourthSchemaObject: PopulatedDoc<IFourthSchema>; // <-- `fourthSchemaObject` can be either ObjectId or `FourthSchema` subdoc
}
const Third = model<IThirdSchema>("Third", ThirdSchema);
export default Third; Which is how we recommend expressing populated docs in our soon-to-be released TypeScript populate docs (see #10212) . |
Fixed by allowing |
- Fixed Mongoose Schema (Replace inheritance structure to intersection) Refer this: Automattic/mongoose#10189 (comment) and this: Automattic/mongoose#10212 (comment)
- Fixed Mongoose Schema (Replace inheritance structure to intersection) Refer this: Automattic/mongoose#10189 (comment) and this: Automattic/mongoose#10212 (comment)
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
After upgrading to use the new builtin types (previously using Mongoose version 5.10.9 and @types/mongoose": "^5.10.3) I am getting the error "Type instantiation is excessively deep and possibly infinite" on the following type of code:
return await Notification.find({ user: user._id }, 'message createdAt link').exec();
The User schema is quite large, but it's never been an issue before for me to find based on the recorded ObjectId this way.
If the current behavior is a bug, please provide the steps to reproduce.
Where the schema is like this:
tsconfig.json:
What is the expected behavior?
No Typescript error
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Versions:
Node: v14.16.0
Mongoose: 5.12.6
MongoDB: 4.2.3
Typescript: 4.2.4
The text was updated successfully, but these errors were encountered: