-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Comments
+1 Models were typed correctly in 5.11.10 but no longer working in 5.11.18 with same error |
IslandRhythms
added
the
typescript
Types or Types-test related issue / Pull Request
label
Mar 1, 2021
The way to work around this with current versions of Mongoose is to pass 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 👍 |
vkarpov15
added a commit
that referenced
this issue
Mar 3, 2021
@vkarpov15 thank you! |
This was referenced Mar 5, 2021
This was referenced Mar 12, 2021
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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
The text was updated successfully, but these errors were encountered: