-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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>>; |
@domharrington I don't think you should use import * as mongoose from 'mongoose'
const childSchema = new mongoose.Schema({ name: String });
const parentSchemaDef = {
// Array of subdocuments
children: [childSchema],
// Single nested subdocuments
child: childSchema
};
const parentSchema = new mongoose.Schema(parentSchemaDef);
type ParentSchema = mongoose.InferRawDocType<typeof parentSchemaDef>;
const Parent = mongoose.model('Parent', parentSchema);
function create(parent: ParentSchema) {
return Parent.create(parent)
}
create({ child: { name: 'child' }, children: [{ name: 'another-child' }] })
|
Oh interesting! I did not know about that one. That seems to do the trick, thank you! |
I tried refactoring my code to use I've subscribed to that issue and will wait until that version gets released before trying again. The error I'm getting is this:
Which I think is the same! |
Yeah that looks like the same issue as #14839. We will ship a fix for that this week. |
Thanks in advance! Appreciate your work on this as always. |
@domharrington your issue should be fixed in v8.6.3 |
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: