Skip to content

Static method types error when declaring model #9969

Closed
@SubJunk

Description

@SubJunk

Do you want to request a feature or report a bug?

Bug (or user error)

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

Hi, I have started to get a TypeScript error with static methods since around 5.11.12, and am trying to fix that. I'm testing on 5.11.17. I am trying to follow the practice described in https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/mongoose#static-methods
I am aware that you are doing your own types now, which is a great move BTW, but that page is the closest I could find to a documented approach.

If I use their example code, but make it valid by adding a field to IUserDocument, I get TypeScript saying:
Property 'static1' is missing in type 'Model<IUserDocument>' but required in type 'IUserModel'..
on the second-to-last line.

The code is:

import {Document, model, Model, Schema} from 'mongoose';

var UserSchema = new Schema({});
UserSchema.statics.static1 = function () { return '' };

interface IUserDocument extends Document {
  instanceField: string;
}
interface IUserModel extends Model<IUserDocument> {
  static1: () => string;
}

var UserModel: IUserModel = model<IUserDocument, IUserModel>('User', UserSchema);
UserModel.static1();    // static methods are available
{
  "compilerOptions": {
    "target": "es2020",
    "module": "commonjs",
    "moduleResolution": "node",
  },
  "files": [
    "./src/interfaces.d.ts"
  ],
  "include": [
    "./**/*"
  ]
}

What is the expected behavior?

That TypeScript would not error.

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

Node 14.15.4
Mongoose 5.11.17
MongoDB 3.6.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions