Skip to content

Commit ca09111

Browse files
authored
Merge pull request #14612 from Automattic/vkarpov15/gh-14601
types: pass DocType down to subdocuments so `HydratedSingleSubdocument` and `HydratedArraySubdocument` `toObject()` returns correct type
2 parents 90f9e55 + 2290c81 commit ca09111

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

test/types/subdocuments.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Schema, model, Model, Document, Types } from 'mongoose';
1+
import {
2+
Schema,
3+
model,
4+
Model,
5+
Document,
6+
Types,
7+
HydratedArraySubdocument,
8+
HydratedSingleSubdocument
9+
} from 'mongoose';
10+
import { expectAssignable } from 'tsd';
211

312
const childSchema: Schema = new Schema({ name: String });
413

@@ -108,3 +117,32 @@ function gh13040(): void {
108117
product.ownerDocument();
109118
});
110119
}
120+
121+
function gh14601() {
122+
interface ISub {
123+
field1: string;
124+
}
125+
interface IMain {
126+
f1: string;
127+
f2: HydratedSingleSubdocument<ISub>;
128+
f3: HydratedArraySubdocument<ISub>[];
129+
}
130+
131+
const subSchema = new Schema({ field1: String }, { _id: false });
132+
133+
const mainSchema = new Schema({
134+
f1: String,
135+
f2: { type: subSchema },
136+
f3: { type: [subSchema] }
137+
});
138+
const MainModel = model<IMain>('Main', mainSchema);
139+
140+
const item = new MainModel({
141+
f1: 'test',
142+
f2: { field1: 'test' },
143+
f3: [{ field1: 'test' }]
144+
});
145+
146+
const f2obj = item.f2.toObject();
147+
expectAssignable<{ _id: Types.ObjectId, field1: string }>(f2obj);
148+
}

types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ declare module 'mongoose' {
157157
>
158158
>
159159
>;
160-
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown> & Require_id<DocType> & TOverrides;
161-
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown> & Require_id<DocType> & TOverrides;
160+
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;
161+
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;
162162

163163
export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
164164
InferSchemaType<TSchema>,

types/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ declare module 'mongoose' {
9696
$parent(): Document;
9797
}
9898

99-
class ArraySubdocument<IdType = any> extends Subdocument<IdType> {
99+
class ArraySubdocument<IdType = any, TQueryHelpers = unknown, DocType = unknown> extends Subdocument<IdType, TQueryHelpers, DocType> {
100100
/** Returns this sub-documents parent array. */
101101
parentArray(): Types.DocumentArray<unknown>;
102102
}

0 commit comments

Comments
 (0)