-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
InferSchemaType produces DocumentArray instead of a simple Array #13772
Comments
"My understanding is that this utility should produce a plain type that has nothing to do with Mongoose": Ideally yes, the |
As expected, going to be a bit hard to change this without #13856. Because |
any workaround for this one? |
Not currently, we're working on a workaround. |
My solution : const likeSchema = new Schema(
{
userId: { type: String, required: true },
date: { type: Date, required: true },
},
{
timestamps: true,
_id: true,
}
);
const postSchema = new Schema(
{
title: { type: String, required: true },
content: { type: String, required: true },
likes: { type: [likeSchema], required: true, default: [] },
},
{
timestamps: true,
_id: true,
}
);
const userSchema = new Schema(
{
_id: { type: mongoose.Schema.Types.ObjectId, required: true },
name: { type: String, required: true },
lastName: { type: String, required: true },
settings: {
type: {
isPublic: { type: Boolean, required: true, default: false },
isForSale: { type: Boolean, required: true, default: false },
},
required: true,
default: {},
},
age: { type: Number, required: false },
posts: {
type: [postSchema],
required: true,
default: [],
},
},
{
timestamps: true,
_id: true,
}
);
type ReplaceMongooseDocumentArrayByArray<MySchema extends Record<string, any>> = {
[K in keyof MySchema]: MySchema[K] extends mongoose.Types.DocumentArray<infer ArrayType>
? ReplaceMongooseDocumentArrayByArray<Omit<ArrayType, keyof mongoose.Types.Subdocument>>[]
: MySchema[K] extends number | string | boolean | Date | mongoose.Schema.Types.ObjectId | mongoose.Types.ObjectId
? MySchema[K]
: ReplaceMongooseDocumentArrayByArray<MySchema[K]>;
};
type User = ReplaceMongooseDocumentArrayByArray<InferSchemaType<typeof userSchema>>; |
Prerequisites
Mongoose version
7.4.4
Node.js version
16.19.1
MongoDB server version
5
Typescript version (if applicable)
4.8.3
Description
The
InferSchemaType
utility converts subdocument arrays intoDocumentArray
s. My understanding is that this utility should produce a plain type that has nothing to do with Mongoose, so it should infer simple arrays instead ofDocumentArray
s. Is my expectation incorrect?Possibly related: #12030 .
Steps to Reproduce
Type
IMySchema
is:Expected Behavior
I'd expect type
IMySchema
to be:The text was updated successfully, but these errors were encountered: