Skip to content

Automatic Type Inference breaks on nested paths #15401

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

Open
2 tasks done
lobabob opened this issue May 3, 2025 · 0 comments
Open
2 tasks done

Automatic Type Inference breaks on nested paths #15401

lobabob opened this issue May 3, 2025 · 0 comments

Comments

@lobabob
Copy link

lobabob commented May 3, 2025

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.10.0

Node.js version

22.13.1

MongoDB server version

8.0.4

Typescript version (if applicable)

5.7.3

Description

According to the docs (https://mongoosejs.com/docs/subdocs.html#subdocuments-versus-nested-paths), nested paths are something that are guaranteed to exist on the model.

But the type inference shows that nested paths are optional which isn't correct afaik.

Steps to Reproduce

  1. Create a Schema with a nested path and make a model for it
export const ExampleSchema = new Schema({
  field1: {
    type: String,
    required: true,
  },
  field2: {
    subfield1: String,
  },
}, { timestamps: true })

export default mongoose.model('example', ExampleSchema)
  1. Create a Document for that Model
const ExampleDoc = new ExampleModel()
  1. Look at the type of the doc you generated. You'll see that field2 in our example shows up as optional when it's supposed to be guaranteed.

Expected Behavior

When using a nested path in a schema, I expect the type inference to show that nested path as something required and not optional.

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

No branches or pull requests

1 participant