Closed aaronyu6 closed 2 years ago
I will paste full content of the generated interface.ts file in next reply
/ 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';
/**
CddStatDocument.toObject()
.
*/
export type CddStatScoreArr = {
testCatId: string;
testCatName: string;
testCatData: Map<
string,
{
type: {};
required: {};
}
>;
_id: mongoose.Types.ObjectId;
};
/**
CddStatDocument.toObject()
. To avoid conflicts with model names, use the type alias CddStatObject
.
*/
export type CddStat = {
schemaVer: string;
cddSn: string;
cddName: string;
cddInfo?: Map<string, string>;
testDate?: Date;
createdDate?: Date;
scoreArr: CddStatScoreArr[];
_id: mongoose.Types.ObjectId;
};
/**
CddStat
)
*/
export type CddStatObject = CddStat;
/**
*/
export type CddStatQueries = {};
export type CddStatMethods = {};
export type CddStatStatics = {};
/**
*/
export type CddStatModel = mongoose.Model<CddStatDocument, CddStatQueries> &
CddStatStatics;
/**
*/
export type CddStatSchema = mongoose.Schema<CddStatDocument, CddStatModel>;
/**
CddStatDocument["scoreArr"]
element.
*/
export type CddStatScoreArrDocument = mongoose.Types.Subdocument & {
testCatId: string;
testCatName: string;
testCatData: mongoose.Types.Map<{
type: {};
required: {};
}>;
_id: mongoose.Types.ObjectId;
};/**
*/
export type CddStatDocument = mongoose.Document<
mongoose.Types.ObjectId,
CddStatQueries
> &
CddStatMethods & {
schemaVer: string;
cddSn: string;
cddName: string;
cddInfo?: mongoose.Types.Map<string>;
testDate?: Date;
createdDate?: Date;
scoreArr: mongoose.Types.DocumentArray<CddStatScoreArrDocument>;
_id: mongoose.Types.ObjectId;
};
/**
*/
export function IsPopulated<T>(doc: T | mongoose.Types.ObjectId): doc is T {
return doc instanceof mongoose.Document;
}
/**
PopulatedDocument
. Returns the parent property of a stringfriend.user
-> friend
)
*/
type ParentProperty${infer P}.${string}
? P : never;/**
PopulatedDocument
. Returns the child property of a stringfriend.user
-> user
).
*/
type ChildProperty${string}.${infer C}
? C : never;/**
PopulatedDocument
. Removes the ObjectId
from the general union type generatedmongoose.Types.ObjectId | UserDocument
-> UserDocument
)
*/
type PopulatedProperty<Root, T extends keyof Root> = Omit<Root, T> & {
[ref in T]: Root[T] extends mongoose.Types.Array/**
*/
export type PopulatedDocument<DocType, T> = T extends keyof DocType
? PopulatedProperty<DocType, T>
: ParentProperty<T> extends keyof DocType
? Omit<DocType, ParentProperty<T>> & {
[ref in ParentProperty<T>]: DocType[ParentProperty<T>] extends mongoose.Types.Array<
infer U
>
? mongoose.Types.Array<
ChildProperty<T> extends keyof U
? PopulatedProperty<U, ChildProperty<T>>
: PopulatedDocument<U, ChildProperty<T>>
>
: ChildProperty<T> extends keyof DocType[ParentProperty<T>]
? PopulatedProperty<DocType[ParentProperty<T>], ChildProperty<T>>
: PopulatedDocument<DocType[ParentProperty<T>], ChildProperty<T>>;
}
: DocType;
/**
/**
Augment mongoose with Query.populate overloads
*/
declare module 'mongoose' {
interface Query<ResultType, DocType extends Document, THelpers = {}> {
populate
& THelpers;
populate
& THelpers; } }
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:
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
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?
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.
Thanks for the info! Will investigate further
@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.
Whoops, sorry for not responding! I am still seeing this mongoose@6.1.8 and mongoose-tsgen@8.4.8.
@joshgachnang Thanks for the info! Any chance you can provide a simple repro project? Havent had much luck reproducing this.
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.
+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
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.
@francescov1 Works for me! Thanks so much for the speedy fix! 🤩
Awesome, thank you!! I'll test out the fix when I'm back in office Tuesday!
@joshgachnang closing this off now, feel free to re-open it if you are still seeing this issue!
Ah ha, this did work!! Thank you so much!
Happy to help! Let me know if you have any suggestions for the package, always looking to improve it 🚀
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
a long error appear
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