-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Properties with unknown type in the mongoose model when set timestamps to true in v6.8.0 #12807
Comments
|
Please try the following to reproduce the issue:
|
I've attached an image to my previous comment. Further:
|
When you switch back to v6.7.5 the problem disappears. |
Confirmed that the following script fails to compile due to import { Schema, createConnection } from 'mongoose';
const userSchema = new Schema(
{
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
},
{
methods: {
comparePassword: function (candidatePassword: string): Promise<boolean> {
const user = this;
return new Promise((resolve, reject) => {
resolve(true);
});
},
},
timestamps: true,
}
);
const connection = createConnection('mongodb://localhost:27017/test');
const Models = {
Users: connection.model("users", userSchema),
};
const user = new Models.Users({ name: 'foo', password: 'bar', email: 'baz' });
const name: string = user.name;
async function foo() {
const user = await Models.Users.findOne().orFail();
const name: string = user.name;
} |
I haven't been able to fix this issue yet, but I found an ok workaround using const userSchema = new Schema(
{
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
},
{
methods: {
comparePassword: function (candidatePassword: string): Promise<boolean> {
const user = this;
return new Promise((resolve, reject) => {
resolve(true);
});
},
},
timestamps: true,
} as { timestamps: true, methods: any } // <-- I guess this is enough to convince TS that `timestamps` is actually true?
); @Code-Mythos can you confirm whether this |
@vkarpov15 Thank you for your reply. |
fix(types): avoid inferring timestamps if `methods`, `virtuals`, or `statics` set
Prerequisites
Mongoose version
6.8.0
Node.js version
16.15.1
MongoDB server version
6.0
Typescript version (if applicable)
4.9.4
Description
In version 6.8.0, when I set the timestamps option to true in a schema, the created model has properties with unknown types that will cause a lot of problems in implementing the application. However, when I switch back to version 6.7.5 the problem disappears.
Version 6.8.0:
When switched back to version 6.7.5 without any modification:
Steps to Reproduce
Considering you are using typescript:
Expected Behavior
Or:
The text was updated successfully, but these errors were encountered: