Skip to content

Commit 6838d48

Browse files
authored
Merge pull request Automattic#15240 from Automattic/vkarpov15/Automatticgh-15211
docs(typescript): recommend using HydratedSingleSubdocument over Types.Subdocument
2 parents ab5ceb4 + 776e9f2 commit 6838d48

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

docs/typescript/subdocuments.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ Define a separate `THydratedDocumentType` and pass it as the 5th generic param t
3838
`THydratedDocumentType` controls what type Mongoose uses for "hydrated documents", that is, what `await UserModel.findOne()`, `UserModel.hydrate()`, and `new UserModel()` return.
3939

4040
```ts
41+
import { HydratedSingleSubdocument } from 'mongoose';
42+
4143
// Define property overrides for hydrated documents
4244
type THydratedUserDocument = {
43-
names?: mongoose.Types.Subdocument<Names>
45+
names?: HydratedSingleSubdocument<Names>
4446
}
4547
type UserModelType = mongoose.Model<User, {}, {}, {}, THydratedUserDocument>;
4648

@@ -51,6 +53,7 @@ const UserModel = mongoose.model<User, UserModelType>('User', userSchema);
5153

5254
const doc = new UserModel({ names: { _id: '0'.repeat(24), firstName: 'foo' } });
5355
doc.names!.ownerDocument(); // Works, `names` is a subdocument!
56+
doc.names!.firstName; // 'foo'
5457
```
5558

5659
## Subdocument Arrays
@@ -81,4 +84,5 @@ const UserModel = model<User, UserModelType>('User', new Schema<User, UserModelT
8184

8285
const doc = new UserModel({});
8386
doc.names[0].ownerDocument(); // Works!
87+
doc.names[0].firstName; // string
8488
```

0 commit comments

Comments
 (0)