Typescript: Type instantiation is excessively deep and possibly infinite
See original GitHub issueDo 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:
import mongoose from 'mongoose';
import { IUser } from './user';
const NotificationSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
message: { type: String, required: true },
link: { type: String, required: true },
},
{
timestamps: true,
},
);
export interface INotification extends mongoose.Document, mongoose.SchemaTimestampsConfig {
user: IUser;
message: string;
}
const Notification = mongoose.model<INotification>('Notification', NotificationSchema);
export default Notification;
tsconfig.json:
{
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"outDir": "dist",
"strict": true,
"alwaysStrict": true,
"typeRoots": [
"typings",
"./node_modules/@types"
],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
},
"include": [
"src/**/*",
"typings"
],
}
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
Issue Analytics
- State:
- Created 2 years ago
- Comments:8
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
UpdateQuery
type and its use of@types/mongodb
’sOnlyFieldsOfType
helper. Not sure why that particular helper causes this issue, we’ll keep digging and see what else we can find.However, we’ve managed to find a couple of workarounds. One workaround we’ve managed to find is to not use
extends Document
. Rewritingfourth.ts
to the below works fine:Based on our experience working with TypeScript so far, we recommend not using
extends Document
anymore, so this change isn’t unreasonable.Another workaround is to use
PopulatedDoc<>
. For example, below is athird.ts
that compiles successfully:Which is how we recommend expressing populated docs in our soon-to-be released TypeScript populate docs (see #10212) .
Fixed by allowing
any
for all update operators, which is something we’ve been meaning to do anyway.