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

Type error if an object with type 'any' is passed to the model's create method (Typescript) #11970

Closed
2 tasks done
artemkosenko opened this issue Jun 22, 2022 · 1 comment
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@artemkosenko
Copy link

Prerequisites

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

Mongoose version

6.4.0

Node.js version

16.15.0

MongoDB server version

5.x

Description

The 'create' method returns wrong object type if the prototype passed to it is of type 'any'

Steps to Reproduce

A code snippet to reproduce.

export interface ImportantObject extends BaseSchema {
    field_one: string;
    field_two: string;
}

const importantObjectSchema = new Schema({
    field_one: {type: String, required: true},
    field_two: {type: String, required: true},
}, {_id: false});

export const ImportantObjectModel = mongoose.model<ImportantObject>("important_objects", importantObjectSchema);


const createOne = async (field_one: string, field_two: string): Promise<ImportantObject> => {
    /* any type is to reproduce the issue */
    const proto: any = {
        field_one,
        field_two
    }
    /*
     TS2740: Type 'Document  { _id: any; }>>> & Omit { _id: any; }> & { _id: ObjectId; }' is missing the following properties from type 'ImportantObject': field_one, field_two, owner, group, and 8 more.
    */
    const new_one: ImportantObject = await ImportantObjectModel.create(proto);
    return new_one;
}

image

Workaround

  1. Stick to mongoose ~6.3.x
  2. add // @ts-ignore

Expected Behavior

A correct type must be returned.

@vkarpov15 vkarpov15 added this to the 6.4.2 milestone Jun 25, 2022
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jun 25, 2022
@vkarpov15
Copy link
Collaborator

I'm sorry for the issue, it looks like this bug was introduced in #11563, and fixed by 9ba3f12 with #12001. I can confirm that the provided script fails to compile with 6.4.2, but succeeds with 9ba3f12. Fix will be in 6.4.2.

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
Development

No branches or pull requests

2 participants