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

InferRawDocType not same type return from .lean() #14839

Closed
2 tasks done
mezozawahra opened this issue Aug 28, 2024 · 3 comments · Fixed by #14882
Closed
2 tasks done

InferRawDocType not same type return from .lean() #14839

mezozawahra opened this issue Aug 28, 2024 · 3 comments · Fixed by #14882
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@mezozawahra
Copy link

mezozawahra commented Aug 28, 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

19.8

MongoDB server version

7.0.12

Typescript version (if applicable)

No response

Description

when I use type returned by InferRawDocType it is supposed to be the same as the type returned from function .lean(), but using the document from then() it throws a type error, reproducsible code below

Steps to Reproduce

import mongoose
const schemaDefinition = {
  email : { 
    type : String ,
    trim: true,
    required : true,
    unique: true,
    lowercase: true
  },
  password : {
    type : String ,
    required : true,
  },
  dateOfBirth: {
    type: Date,
    required: true
  }
}

const UserModel = mongoose.model('User' , UserSchema);
type UserType = mongoose.InferRawDocType< typeof schemaDefinition>;

let user: UserType | null;

async function queryById(id: string ){
    let user: UserType | null ;
    UserModel.findById(id).lean().then( result=>{
        if (result){
            user = result
        }
    })
}

this code gives type error

Type 'FlattenMaps<{ email: string; password: string; firstName: string; lastName: string; dateOfBirth: Date; }> & { _id: ObjectId; }' is not assignable to type '{ email: string; password: string; firstName: string; lastName: string; dateOfBirth: Date; } | null'.
  Type 'FlattenMaps<{ email: string; password: string; firstName: string; lastName: string; dateOfBirth: Date; }> & { _id: ObjectId; }' is not assignable to type '{ email: string; password: string; firstName: string; lastName: string; dateOfBirth: Date; }'.
    Types of property 'dateOfBirth' are incompatible.
      Type 'Date' is missing the following properties from type 'Date': expires, max, min, defaultOptions, and 21 more.

Also whenever i use user._id it throws error Property '_id' does not exist on type '{ email: string; password: string; firstName: string; lastName: string; dateOfBirth: Date; }

Expected Behavior

No response

@vkarpov15
Copy link
Collaborator

Is there somewhere in the Mongoose docs where it indicates that InferRawDocType should match the return value from lean()?

@mezozawahra
Copy link
Author

@vkarpov15
in the doc it says

If you need to explicitly get the raw document type (the value returned from doc.toObject(), await Model.findOne().lean(), etc.) from your schema definition, you can use Mongoose's inferRawDocType helper as follows:

Tbh,I am not sure if it's just a bug or I misunderstood it either way thank you for your time

@vkarpov15
Copy link
Collaborator

Ah you're right, I misunderstood. We'll investigate.

@vkarpov15 vkarpov15 added this to the 8.6.1 milestone Aug 28, 2024
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 28, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.6.1, 8.6.2 Sep 3, 2024
vkarpov15 added a commit that referenced this issue Sep 11, 2024
type(inferrawdoctype): infer Date types as JS dates rather than Mongoose SchemaType Date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
2 participants