Skip to content

Commit

Permalink
Make standalone test for virtuals auto types.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Jul 19, 2022
1 parent ebc928a commit 67b1796
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
28 changes: 1 addition & 27 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
SchemaType,
Query,
HydratedDocument,
SchemaOptions,
InferDocType,
FlatRecord
SchemaOptions
} from 'mongoose';
import { expectType, expectError, expectAssignable } from 'tsd';

Expand Down Expand Up @@ -467,10 +465,6 @@ export function autoTypedSchema() {
type: String,
required: [true, 'userName is required']
},
email: {
type: String,
required: [true, 'email is required']
},
description: String,
nested: new Schema({
age: {
Expand Down Expand Up @@ -523,18 +517,6 @@ export function autoTypedSchema() {
expectAssignable<Query<unknown, AutoTypedSchemaType['schema']>>(this);
return this.where({ userName });
}
},
virtuals: {
domain: {
get() {
expectType<Document<any, any, AutoTypedSchemaType['schema']> & AutoTypedSchemaType['schema']>(this);
return this.email.slice(this.email.indexOf('@') + 1);
},
set() {
expectType<Document<any, any, AutoTypedSchemaType['schema']> & AutoTypedSchemaType['schema']>(this);
},
options: {}
}
}
});

Expand All @@ -544,17 +526,12 @@ export function autoTypedSchema() {

expectError<AutoTypedSchemaType['schema'] & { doesNotExist: boolean; }>({} as InferredSchemaType);

type InferredDocType = InferDocType<typeof AutoTypedSchema>;

expectType<FlatRecord<AutoTypedSchemaType['schema'] & AutoTypedSchemaType['virtuals']>>({} as InferredDocType);

return AutoTypedSchema;
}

export type AutoTypedSchemaType = {
schema: {
userName: string;
email: string;
description?: string;
nested?: {
age: number;
Expand All @@ -573,9 +550,6 @@ export type AutoTypedSchemaType = {
},
methods: {
instanceFn: () => 'Returned from DocumentInstanceFn'
},
virtuals: {
domain: string
}
};

Expand Down
35 changes: 31 additions & 4 deletions test/types/virtuals.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Document, Model, Schema, model } from 'mongoose';
import { Document, Model, Schema, model, InferSchemaType, InferDocumentType, FlatRecord } from 'mongoose';
import { expectType } from 'tsd';
import { autoTypedModel } from './models.test';

interface IPerson {
_id: number;
Expand Down Expand Up @@ -89,7 +88,35 @@ function gh11543() {
}

function autoTypedVirtuals() {
const AutoTypedModel = autoTypedModel();
const doc = new AutoTypedModel();
type AutoTypedSchemaType = InferSchemaType<typeof testSchema>;
type VirtualsType = { domain: string };
type InferredDocType = InferDocumentType<typeof testSchema>;

const testSchema = new Schema({
email: {
type: String,
required: [true, 'email is required']
}
}, {
virtuals: {
domain: {
get() {
expectType<Document<any, any, { email: string }> & AutoTypedSchemaType>(this);
return this.email.slice(this.email.indexOf('@') + 1);
},
set() {
expectType<Document<any, any, AutoTypedSchemaType> & AutoTypedSchemaType>(this);
},
options: {}
}
}
});


const TestModel = model('AutoTypedVirtuals', testSchema);

const doc = new TestModel();
expectType<string>(doc.domain);

expectType<FlatRecord<AutoTypedSchemaType &VirtualsType >>({} as InferredDocType);
}
4 changes: 2 additions & 2 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ declare module 'mongoose' {
* @param {SchemaType} SchemaType A generic of schema type instance.
* @example
* const userSchema = new Schema({userName:String});
* type UserType = InferDocType<typeof userSchema>;
* type UserType = InferDocumentType<typeof userSchema>;
* // result
* type UserType = {userName?: string}
*/
type InferDocType<SchemaType> = FlatRecord<InferSchemaType<SchemaType> & ObtainSchemaGeneric<SchemaType, 'TVirtuals'>>;
type InferDocumentType<SchemaType> = FlatRecord<InferSchemaType<SchemaType> & ObtainSchemaGeneric<SchemaType, 'TVirtuals'>>;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down

0 comments on commit 67b1796

Please sign in to comment.