Skip to content
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

Static method types error when declaring model #9969

Closed
SubJunk opened this issue Feb 25, 2021 · 3 comments
Closed

Static method types error when declaring model #9969

SubJunk opened this issue Feb 25, 2021 · 3 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@SubJunk
Copy link

SubJunk commented Feb 25, 2021

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

@lele0108
Copy link

+1 Models were typed correctly in 5.11.10 but no longer working in 5.11.18 with same error

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Mar 1, 2021
@vkarpov15 vkarpov15 added this to the 5.11.19 milestone Mar 1, 2021
@vkarpov15
Copy link
Collaborator

The way to work around this with current versions of Mongoose is to pass IUserDocument and IUserModel as generics to the schema:

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

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

var UserSchema = new Schema<IUserDocument, IUserModel>({});
UserSchema.statics.static1 = function () { return '' };

var UserModel: IUserModel = model<IUserDocument, IUserModel>('User', UserSchema);
UserModel.static1();    // compiles!

We'll release a fix that will make your code compile as intended in v5.11.19 👍

@SubJunk
Copy link
Author

SubJunk commented Mar 3, 2021

@vkarpov15 thank you!

This was referenced Mar 5, 2021
This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

4 participants