Skip to content
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

fix(types): avoid adding non-existent properties from model constructor for typegoose #11986

Merged
merged 2 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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