Skip to content

Properties with unknown type in the mongoose model when set timestamps to true in v6.8.0 #12807

Open
@Code-Mythos

Description

@Code-Mythos

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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:

(property) Users: Model<{
    createdAt: NativeDate;
    updatedAt: NativeDate;
} & {
    email?: unknown;
    password?: unknown;
    name?: unknown;
}, {}, {
    // This is a custom method in the schema
    comparePassword: (this: Document<unknown, any, FlatRecord<{
        email: string;
        password: string;
        name: string;
...

unknown-types-2

When switched back to version 6.7.5 without any modification:

(property) Users: Model<{
    email: string;
    password: string;
    name: string;
}, {}, {
    // This is a custom method in the schema
    comparePassword: (this: Document<unknown, any, FlatRecord<{
        email: string;
        password: string;
        name: string;
...

Steps to Reproduce

Considering you are using typescript:

  1. Install version 6.8.0 : npm i mongoose@6.8.0
  2. Create a schema with some properties and set the timestamps option to true.
  3. Create a connection using mongoose.createConnection.
  4. Create a model for the schema using the connection.model.
  5. Hover over the created model to see the type of the model.
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) => {
          compare(candidatePassword, user.password, (err, isMatch) => {
            if (err) return reject(err);

            return resolve(isMatch);
          });
        });
      },
    },

    timestamps: true,
  }
);

const connection = createConnection(config.authDatabaseURL);

const Models = {
  Users: connection.model("users", userSchema),
};

Expected Behavior

(property) Users: Model<{
    createdAt: NativeDate;
    updatedAt: NativeDate;
} & {
    email: string;
    password: string;
    name: string;
}, {}, {
    // This is a custom method in the schema
    comparePassword: (this: Document<unknown, any, FlatRecord<{
        email: string;
        password: string;
        name: string;
...

Or:

(property) Users: Model<{
    email: string;
    password: string;
    name: string;
}, {}, {
    // This is a custom method in the schema
    comparePassword: (this: Document<unknown, any, FlatRecord<{
        email: string;
        password: string;
        name: string;
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions