francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 24 forks source link

bug and temp solution for error: Type 'DocType' does not satisfy the constraint 'Document<any, any, any>'. #75

Closed aaronyu6 closed 2 years ago

aaronyu6 commented 3 years ago

mongoose-tsgen: cause error:Type 'DocType' does not satisfy the constraint 'Document<any, any, any>'.

Brief: this error only occur when generated interface exist. if delete the interface file, problem disappear.

error condition 1

mongoose version 6.0.9 latest and an interface file is generated from mtgen. start nestjs

npm run start:dev

a long error appear

node_modules/mongoose/index.d.ts:944:193 - error TS2344: Type 'EnforceDocument<T, TMethods>' does not satisfy the constraint 'Document<any, any, any>'.
  Type 'Require_id<T> | (Document<any, any, T> & Require_id<T> & TMethods)' is not assignable to type 'Document<any, any, any>'.
    Type 'Require_id<T>' is not assignable to type 'Document<any, any, any>'.
      Type '(T & { _id: T["_id"]; }) | (T & { _id: ObjectId; })' is not assignable to type 'Document<any, any, any>'.
        Type 'T & { _id: T["_id"]; }' is not assignable to type 'Document<any, any, any>'.
          The types of '$set(...).replaceOne(...).$where(...).and' are incompatible between these types.
            Type '(array: FilterQuery<Document<any, any, any> & T>[]) => Query<(Document<any, any, any> & T)[], Document<any, any, any> & T, {}, Document<...> & T>' is not assignable to type '(array: FilterQuery<Document<any, any, any>>[]) => Query<Document<any, any, any>[], Document<any, any, any>, {}, Document<any, any, any>>'.
              Types of parameters 'array' and 'array' are incompatible.
                Type 'FilterQuery<Document<any, any, any>>[]' is not assignable to type 'FilterQuery<Document<any, any, any> & T>[]'.
                  Type 'FilterQuery<Document<any, any, any>>' is not assignable to type 'FilterQuery<Document<any, any, any> & T>'.
                    Type 'FilterQuery<Document<any, any, any>>' is not assignable to type '{ [P in keyof (Document<any, any, any> & T)]?: P extends "_id" ? [Extract<(Document<any, any, any> & T)[P], ObjectId>] extends [never] ? Condition<(Document<any, any, any> & T)[P]> : Condition<...> : [...] extends [...] ? Condition<...> : Condition<...>; }'.
                      Type 'Document<any, any, any> & { _id: any; }' is not assignable to type 'Document<any, any, any>'.
                        The types of 'replaceOne(...).$where(...).and' are incompatible between these types.
                          Type '(array: FilterQuery<T>[]) => Query<T[], T, {}, T>' is not assignable to type '(array: FilterQuery<Document<any, any, any>>[]) => Query<Document<any, any, any>[], Document<any, any, any>, {}, Document<any, any, any>>'.
                            Types of parameters 'array' and 'array' are incompatible.
                              Type 'FilterQuery<Document<any, any, any>>[]' is not assignable to type 'FilterQuery<T>[]'.
                                Type 'FilterQuery<Document<any, any, any>>' is not assignable to type 'FilterQuery<T>'.
                                  Type 'FilterQuery<Document<any, any, any>>' is not assignable to type '{ [P in keyof T]?: P extends "_id" ? [Extract<T[P], ObjectId>] extends [never] ? Condition<T[P]> : Condition<string | { _id: ObjectId; } | T[P]> : [...] extends [...] ? Condition<...> : Condition<...>; }'.

944     findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): Query<any, EnforceDocument<T, TMethods>, T>;

error condition 2

if environment combination changed to: mongoose version 5.13.10 and an interface file is generated.

a single error

if remove the generated interface file, problem disappear, both mongoose latest 5.3.10 and latest 6.0.9 work properly after starting up nestjs with npm run start:dev

analysis conclusion:

The generated interface file is conflicted/incompatible with nestjs normal start up. it also generate same errors when I run npx typedoc to generate typescript document.

Temporary walk around

temporary walk around method is after generation of interface file, copy the needed portion of interface code from interface file to a self-made interface file, and delete auto generated interface file.

system: WSL2 ubuntu 18.04 LTS my npm ls for recreating problem reference

├── @nestjs/cli@7.6.0
├── @nestjs/common@7.6.18
├── @nestjs/core@7.6.18
├── @nestjs/mapped-types@1.0.0
├── @nestjs/platform-express@7.6.18
├── @nestjs/schematics@7.3.1
├── @nestjs/testing@7.6.18
├── @types/express@4.17.13
├── @types/jest@26.0.24
├── @types/mongoose@5.11.97
├── @types/node@14.17.15
├── @types/supertest@2.0.11
├── @typescript-eslint/eslint-plugin@4.31.1
├── @typescript-eslint/parser@4.31.1
├── eslint-config-prettier@8.3.0
├── eslint-plugin-prettier@3.4.1
├── eslint@7.32.0
├── express@4.17.1
├── fs@0.0.1-security
├── jest@26.6.3
├── mongoose-tsgen@8.4.6
├── mongoose@5.13.10
├── prettier@2.4.0
├── reflect-metadata@0.1.13
├── rimraf@3.0.2
├── rxjs@6.6.7
├── supertest@6.1.6
├── ts-jest@26.5.6
├── ts-loader@8.3.0
├── ts-node@10.2.1
├── tsconfig-paths@3.11.0
├── typedoc@0.22.5
└── typescript@4.4.3
aaronyu6 commented 3 years ago

I will paste full content of the generated interface.ts file in next reply

aaronyu6 commented 3 years ago

/ tslint:disable / / eslint-disable /

// ######################################## THIS FILE WAS GENERATED BY MONGOOSE-TSGEN ######################################## //

// NOTE: ANY CHANGES MADE WILL BE OVERWRITTEN ON SUBSEQUENT EXECUTIONS OF MONGOOSE-TSGEN.

import mongoose from 'mongoose';

/**

/**

/**

/**

export type CddStatMethods = {};

export type CddStatStatics = {};

/**

/**

/**

/**

/**

/**

/**

/**

/**

/**

/**

francescov1 commented 3 years ago

Hey @aaronyu6 ,

Thanks for reporting! One thing comes to mind off the top of my head, I see you have @types/mongoose installed. These are legacy community Mongoose types which conflict with newer mongoose types. If using a version of Mongoose greater than 5.11.0, ensure to uninstall these types.

If that doesn't help, I can investigate a bit, but note that this is not tested with NestJS. Can you provide a bit more info:

aaronyu6 commented 3 years ago

version of mongoose-tsgen "node_modules/mongoose-tsgen": { "version": "8.4.6", no code snippet shown related to casuing the error. it occur during nestjs startup

francescov1 commented 3 years ago

Hi @aaronyu6

I'm sorry I need more information to help out here. If you could create a simple repo to reproduce the issue then I can investigate, but I've never seen such a problem on my end.

Did uninstalling @types/mongoose help at all?

joshgachnang commented 3 years ago

FWIW, I hit this error running mongoose-tsgen@8.x.x with mongoose@5.13.13. Switching to mongoose-tsgen@7.x.x cleared it up.

francescov1 commented 3 years ago

Thanks for the info! Will investigate further

francescov1 commented 2 years ago

@joshgachnang are you still seeing this error with the latest version of mongoose-tsgen? A few version fixes were included there.

At my company we have used mongoose@5.12.10 with both mongoose-tsgen@8.4.6 & mongoose-tsgen@8.4.7, no such issues encountered. Have also had some usage with mongoose@6 and similarly no issues.

joshgachnang commented 2 years ago

Whoops, sorry for not responding! I am still seeing this mongoose@6.1.8 and mongoose-tsgen@8.4.8.

francescov1 commented 2 years ago

@joshgachnang Thanks for the info! Any chance you can provide a simple repro project? Havent had much luck reproducing this.

joshgachnang commented 2 years ago

Yeah I'll try and figure out a repro. I have another project (newly started, rather than a 6+ year old one) that is using these same versions and doesn't have the issue. So I'm sure it's somewhere in my dependencies or something.

dkoo761 commented 2 years ago

+1

I'm having this same error as well

node_modules/mongoose/index.d.ts:2124:103 - error TS2344: Type 'DocType' does not satisfy the constraint 'Document<any, any, any>'.

If I comment out the last function in the generated code file (I can't remember the function name but something like "populate" - it's the function that "augments mongoose") when using a single model then the error goes away, so that might help with debugging.

mongo = 5.0.5 mongoose = 6.1.8 mongoose-tsgen = 8.4.8 typescript = 4.5.5 node = 14.18.3

francescov1 commented 2 years ago

Hey @dkoo761 @joshgachnang thanks for all the help here. I believe I've finally hunted down this bug and fixed it here 🎉 The Mongoose library types are so overcomplicated and constantly changing 😅

The updates are live in mongoose-tsgen 8.4.10. Can you guys confirm that the issue has been resolved? I was able to repro it in a seemingly random way. so I want to ensure the root problem is fixed before we close this.

dkoo761 commented 2 years ago

@francescov1 Works for me! Thanks so much for the speedy fix! 🤩

joshgachnang commented 2 years ago

Awesome, thank you!! I'll test out the fix when I'm back in office Tuesday!

francescov1 commented 2 years ago

@joshgachnang closing this off now, feel free to re-open it if you are still seeing this issue!

joshgachnang commented 2 years ago

Ah ha, this did work!! Thank you so much!

francescov1 commented 2 years ago

Happy to help! Let me know if you have any suggestions for the package, always looking to improve it 🚀