mongoose: Typescript Schema definition is incorrect

version 5.11.0 mongoose

Reporting a bug

cannot pass two parameters to new Schema ();


 error TS2554: Expected 0-1 arguments, but got 2.

20     const ModelSchema = new Schema(modelSettings.fields,{
                                                           ~
21         ...defaultSchemaOptions,
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22         ...modelSettings.schemaOptions,
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23     });

What is the expected behavior?

Schema should accept two parameters.

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

version 5.11.0 mongoose version 12.18.3 node

the fault lies in mongoose/index.d.ts

    /**
     * Create a new schema
     */
    constructor(definition?: SchemaDefinition);

the @types/mongoose/index.d.ts has the correct constructor definition.

  class Schema<T = any> extends events.EventEmitter {
    /**
     * Schema constructor.
     * When nesting schemas, (children in the example above), always declare
     * the child schema first before passing it into its parent.
     * @event init Emitted after the schema is compiled into a Model.
     */
    constructor(definition?: SchemaDefinition, options?:SchemaOptions  );

note the “options?:SchemaOptions”.

my code is picking up the mongoose/index.d.ts, not @types but that’s another battle.

thanks

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 19 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Mongoose 5.11.0 released it’s own official types file (#8108). Are there any docs on how to migrate from using @types/mongoose to official types?

i had this problem and i solved it by fixing versions on my package.json: “devDependencies”: { “@types/mongoose”: “5.10.1”, …

“dependencies”: { “mongoose”: “5.10.1”, …

i have to use old 5.10 since 5.11 is ruining my work.

We’re working on fixes for any issues that pop up - in theory you shouldn’t have any issues migrating from @types/mongoose to the new bindings. If you find any, please open a new issue with code samples, #9608 is a good example.

@simllll it looks like you’re experiencing a problem because you’re importing both @types/mongoose and Mongoose v5.11’s type bindings. Can you try removing @types/mongoose and see if you still get compiler errors?

Changing “mongoose”: “^5.x.xx” to “mongoose”: “~5.x.xx” in package.json will solve these problems for now I guess.

I’m experiencing this one too. Have fixed my mongoose version at 15.10.3 for now while I watch this thread 😃