Skip to content

Commit 4f89f23

Browse files
authored
Merge pull request #11986 from Automattic/vkarpov15/gh-11960
fix(types): avoid adding non-existent properties from model constructor for typegoose
2 parents b66cdc6 + 5753bce commit 4f89f23

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"rules": {
2626
"@typescript-eslint/triple-slash-reference": "off",
27+
"@typescript-eslint/no-non-null-assertion": "off",
2728
"spaced-comment": [
2829
"error",
2930
"always",

test/types/document.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,49 @@ function autoTypedDocument() {
192192
expectType<AutoTypedSchemaType['schema']['userName']>(AutoTypeModelInstance.userName);
193193
expectType<AutoTypedSchemaType['schema']['favoritDrink']>(AutoTypeModelInstance.favoritDrink);
194194
expectType<AutoTypedSchemaType['schema']['favoritColorMode']>(AutoTypeModelInstance.favoritColorMode);
195-
expectType<number>(AutoTypeModelInstance.unExistProperty);
196-
expectType<number>(AutoTypeModelInstance.description);
197195

198196
// Document-Methods-tests
199197
expectType<ReturnType<AutoTypedSchemaType['methods']['instanceFn']>>(new AutoTypedModel().instanceFn());
200198

199+
}
200+
201+
async function gh11960() {
202+
type DocumentType<T> = Document<any> & T;
203+
type SubDocumentType<T> = DocumentType<T> & Types.Subdocument;
204+
type ArraySubDocumentType<T> = DocumentType<T> & Types.ArraySubdocument;
205+
206+
interface Nested {
207+
dummy?: string;
208+
}
209+
210+
interface Parent {
211+
username?: string;
212+
map?: Map<string, string>;
213+
nested?: SubDocumentType<Nested>;
214+
nestedArray?: ArraySubDocumentType<Nested>[];
215+
}
216+
217+
const NestedSchema = new Schema({
218+
dummy: { type: String }
219+
});
220+
221+
const ParentSchema = new Schema({
222+
username: { type: String },
223+
map: { type: Map, of: String },
224+
nested: { type: NestedSchema },
225+
nestedArray: [{ type: NestedSchema }]
226+
});
227+
228+
const ParentModel = model<DocumentType<Parent>>('Parent', ParentSchema);
229+
230+
const doc = new ParentModel({
231+
username: 'user1',
232+
map: { key1: 'value1', key2: 'value2' },
233+
nested: { dummy: 'hello' },
234+
nestedArray: [{ dummy: 'hello again' }]
235+
});
236+
237+
expectType<Map<string, string> | undefined>(doc.map);
238+
doc.nested!.parent();
239+
201240
}

types/models.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ declare module 'mongoose' {
119119
AcceptsDiscriminator,
120120
IndexManager,
121121
SessionStarter {
122-
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<MergeType<T, DocType>, TMethodsAndOverrides, TVirtuals> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
122+
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides, TVirtuals> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
123123

124124
aggregate<R = any>(pipeline?: PipelineStage[], options?: mongodb.AggregateOptions, callback?: Callback<R[]>): Aggregate<Array<R>>;
125125
aggregate<R = any>(pipeline: PipelineStage[], callback?: Callback<R[]>): Aggregate<Array<R>>;

0 commit comments

Comments
 (0)