Skip to content

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

Closed
@mezozawahra

Description

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

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions