Skip to content

Commit

Permalink
Merge pull request #11986 from Automattic/vkarpov15/gh-11960
Browse files Browse the repository at this point in the history
fix(types): avoid adding non-existent properties from model constructor for typegoose
  • Loading branch information
vkarpov15 committed Jun 25, 2022
2 parents b66cdc6 + 5753bce commit 4f89f23
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Expand Up @@ -24,6 +24,7 @@
],
"rules": {
"@typescript-eslint/triple-slash-reference": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"spaced-comment": [
"error",
"always",
Expand Down
43 changes: 41 additions & 2 deletions test/types/document.test.ts
Expand Up @@ -192,10 +192,49 @@ function autoTypedDocument() {
expectType<AutoTypedSchemaType['schema']['userName']>(AutoTypeModelInstance.userName);
expectType<AutoTypedSchemaType['schema']['favoritDrink']>(AutoTypeModelInstance.favoritDrink);
expectType<AutoTypedSchemaType['schema']['favoritColorMode']>(AutoTypeModelInstance.favoritColorMode);
expectType<number>(AutoTypeModelInstance.unExistProperty);
expectType<number>(AutoTypeModelInstance.description);

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

}

async function gh11960() {
type DocumentType<T> = Document<any> & T;
type SubDocumentType<T> = DocumentType<T> & Types.Subdocument;
type ArraySubDocumentType<T> = DocumentType<T> & Types.ArraySubdocument;

interface Nested {
dummy?: string;
}

interface Parent {
username?: string;
map?: Map<string, string>;
nested?: SubDocumentType<Nested>;
nestedArray?: ArraySubDocumentType<Nested>[];
}

const NestedSchema = new Schema({
dummy: { type: String }
});

const ParentSchema = new Schema({
username: { type: String },
map: { type: Map, of: String },
nested: { type: NestedSchema },
nestedArray: [{ type: NestedSchema }]
});

const ParentModel = model<DocumentType<Parent>>('Parent', ParentSchema);

const doc = new ParentModel({
username: 'user1',
map: { key1: 'value1', key2: 'value2' },
nested: { dummy: 'hello' },
nestedArray: [{ dummy: 'hello again' }]
});

expectType<Map<string, string> | undefined>(doc.map);
doc.nested!.parent();

}
2 changes: 1 addition & 1 deletion types/models.d.ts
Expand Up @@ -119,7 +119,7 @@ declare module 'mongoose' {
AcceptsDiscriminator,
IndexManager,
SessionStarter {
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<MergeType<T, DocType>, TMethodsAndOverrides, TVirtuals> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides, TVirtuals> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;

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

0 comments on commit 4f89f23

Please sign in to comment.