-
-
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
Subdocument type is broken? #15211
Comments
Hi, try with this code: // Setup
import { Schema, Types, model, Model, HydratedSingleSubdocument, HydratedDocument, SchemaTypes } from "mongoose";
// Subdocument definition
interface INames {
_id: Types.ObjectId;
firstName: string;
}
interface INamesVirtuals {
id: string;
}
type NamesInstance = HydratedSingleSubdocument<INames, INamesVirtuals>;
type NamesModelType = Model<INames, {}, {}, INamesVirtuals, NamesInstance>
const namesSchema = new Schema<INames, NamesModelType>({
firstName: {
type: SchemaTypes.String,
required: true,
}
})
// Document definition
interface IUser {
names: INames;
}
type UserDocumentOverrides = {
names: NamesInstance;
};
interface IUserVirtuals {
id: string;
}
type UserInstance = HydratedDocument<IUser, UserDocumentOverrides & IUserVirtuals>
// Models and schemas
type UserModelType = Model<IUser, {}, UserDocumentOverrides, IUserVirtuals, UserInstance>;
const userSchema = new Schema<IUser, UserModelType>({
names: {
type: namesSchema,
required: true,
},
});
const UserModel = model<IUser, UserModelType>("User", userSchema);
// Create a new document:
const doc = new UserModel({ names: { _id: "0".repeat(24), firstName: "foo" } });
doc.names.ownerDocument();
doc.names.firstName; |
Thanks a lot for your suggestion @nikzanda . class DocumentArray<T, THydratedDocumentType extends Types.Subdocument<any> = Types.Subdocument<InferId<T>, any, T> & T> extends Types.Array<THydratedDocumentType> { Subdocument however does not do that. class Subdocument<IdType = unknown, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> Here, what the docs suggest is Based on my understanding it does not make sense that the properties are discarded as in the docs. That seems like a bug instead. Also, having to provide a HydratedDocument or HydratedSingleSubdocument to Model's THydratedDocumentType does not make sense to me, Model should take care of that, no? Output of find, new.. is always hydrated, unless with lean, right? |
Try the following approach: // Setup
import { Schema, Types, model, Model, HydratedSingleSubdocument } from "mongoose";
// Subdocument definition
interface Names {
_id: Types.ObjectId;
firstName: string;
}
// Document definition
interface User {
names: Names;
}
type THydratedUserDocument = {
names?: HydratedSingleSubdocument<Names>;
};
// Models and schemas
type UserModelType = Model<User, {}, {}, {}, THydratedUserDocument>;
const userSchema = new Schema<User, UserModelType>({
names: new Schema<Names>({ firstName: String }),
});
const UserModel = model<User, UserModelType>("User", userSchema);
// Create a new document:
const doc = new UserModel({ names: { _id: "0".repeat(24), firstName: "foo" } });
doc.names!.ownerDocument();
doc.names!.firstName;
|
Prerequisites
Mongoose version
8.9.5
Node.js version
22.4.0
MongoDB server version
ts issue
Typescript version (if applicable)
5.3.2
Description
Dear Mongoose Team,
When providing an override for Subdocuments, access to the properties of that subdocument is gone.
I took the example from the docs in the typescript/subdocuments section as a reproduceable example.
Hopefully it's not me being a dummy.
Thanks.
Steps to Reproduce
Expected Behavior
I expect to be able to access .firstName on .names.
The text was updated successfully, but these errors were encountered: