node.jsdatabasemongodbmongoosetypegoose

typegoose/mongoose What is correct way to get new document from model class


I upgrade mongoose version to 6 and i found...

import {
    getModelForClass,
} from "@typegoose/typegoose";

export const ProductModel = getModelForClass(Product); // this give me mongoose model type from my class



const product = new Product();

const A = new ProductModel(product) // A is not DocumentType it is just Product...
const B = new ProductModel({...product}) //  B is DocumentType But why?

  1. Can you explain how is this works?
  2. And what is the correct way to get an DocumentType instance?
  3. I wanted to search the Mongoose doc, but couldn't find a suitable keyword. Would you give me a link and hints..

Solution

  • This Question was also asked in the Github Discussions and resolved there

    TL;DR: This issue of being different types seems to be from typescript 4.3, upgrading to typescript 4.4 fixed it