Skip to content

Conversation

vkarpov15
Copy link
Collaborator

@vkarpov15 vkarpov15 commented Oct 10, 2025

Fix #15681

Summary

Something in 8.19.0 release started causing the following script to OOM crash:

import mongoose from "mongoose";

function getMongooseModel<T>(resName: string): mongoose.Model<T> {
  if (mongoose.models[resName]) {
    return mongoose.models[resName];
  }

  const schema = new mongoose.Schema({
    example: String,
  });

  return mongoose.model<T>(resName, schema);
}

type Example = {
  name: string;
};

const Model = getMongooseModel<Example>("Example");

I haven't been able to figure out exactly what, but there are two easy fixes:

  1. Update user code to do const schema = new mongoose.Schema<T>(...)
  2. Allow model<T> to take in Schema<any> as opposed to Schema<T>

(2) seems reasonable to do, although we will also suggest (1). These fixes unfortunately seem to be exclusive - applying (2) makes (1) fail with OOM :(

Examples

@vkarpov15 vkarpov15 added this to the 8.19.2 milestone Oct 10, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a type signature issue in Mongoose's model<T> function that was causing OOM crashes in TypeScript when using generic models with untyped schemas. The change relaxes the type constraints to accept Schema<any> instead of requiring Schema<T>, allowing developers to create generic model functions without needing to explicitly type their schemas.

Key changes:

  • Relaxed type signature for model<T> to accept any schema type
  • Added test case demonstrating the previously problematic usage pattern

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mongoose 8.19.x causes TypeScript to run out of memory when passing a generic type to mongoose.model

1 participant