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

.populate() clobbers the return type with { [x: string]: NativeDate; } #14825

Closed
2 tasks done
smbogan opened this issue Aug 22, 2024 · 5 comments · Fixed by #14829
Closed
2 tasks done

.populate() clobbers the return type with { [x: string]: NativeDate; } #14825

smbogan opened this issue Aug 22, 2024 · 5 comments · Fixed by #14829
Milestone

Comments

@smbogan
Copy link

smbogan commented Aug 22, 2024

Prerequisites

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

Mongoose version

8.5.3

Node.js version

22.5.1

MongoDB server version

5.0

Typescript version (if applicable)

4.9.5

Description

The Typescript type for populate's return value:

const result = await SomeModel.findOne().populate<{ otherInfo: string }>("other");

includes MergeType<{ [x: string]: NativeDate; } & ....

This causes later code to assume every property is of type NativeDate.

This was observed after upgrading from mongoose 6.12.0 to 8.5.3.

Steps to Reproduce

  1. Create any model.
  2. Call findOne, followed by populate with a type parameter.

Expected Behavior

The types to be properly merged without { [x: string]: NativeDate; } being included.

@ark23BLR
Copy link

ark23BLR commented Aug 25, 2024

@smbogan Does SomeModel in your example is constructed with timestamps schema options? Can you provide how your schema was defined?

@smbogan
Copy link
Author

smbogan commented Aug 25, 2024

@smbogan Does SomeModel in your example is constructed with timestamps schema options?

Yes.

{
    timestamps: {
      createdAt: "createdAt",
      updatedAt: "updatedAt",
    },
}

@ark23BLR
Copy link

ark23BLR commented Aug 25, 2024

@smbogan You can try the following

{
    timestamps: {
      createdAt: "createdAt",
      updatedAt: "updatedAt",
    },
} as const

Alternatively, you can provide it like this:

{
    timestamps: {
      createdAt: true,
      updatedAt: true,
    },
}

Other than that

{
    timestamps: true,
}

The reason of the issue is that you provide createdAt, updatedAt values like string in typescript type, not as a literal and based on the values schema type is built. With using as const you provide the values as literal and mongoose can build a correct type.

Does it help?

@smbogan
Copy link
Author

smbogan commented Aug 25, 2024

Using as const did fix the types. Thank you!

@ark23CIS
Copy link
Contributor

ark23CIS commented Aug 25, 2024

@vkarpov15 Created an MR to not confuse users in case if they provide timestamp options like in case that was mentioned above

@vkarpov15 vkarpov15 added this to the 8.5.5 milestone Aug 26, 2024
vkarpov15 added a commit that referenced this issue Aug 26, 2024
fix: fixing schema type based on timestamps schema options value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants