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

Feature/enhance auto typed virtuals #12702

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions test/types/virtuals.test.ts
Expand Up @@ -89,7 +89,7 @@ function gh11543() {

function autoTypedVirtuals() {
type AutoTypedSchemaType = InferSchemaType<typeof testSchema>;
type VirtualsType = { domain: string };
type SchemaWithoutVirtual = Omit<AutoTypedSchemaType, 'domain'>;
type InferredDocType = FlatRecord<AutoTypedSchemaType & ObtainSchemaGeneric<typeof testSchema, 'TVirtuals'>>;

const testSchema = new Schema({
Expand All @@ -101,11 +101,11 @@ function autoTypedVirtuals() {
virtuals: {
domain: {
get() {
expectType<Document<any, any, { email: string }> & AutoTypedSchemaType>(this);
expectType<Document<any, any, { email: string }>>(this);
return this.email.slice(this.email.indexOf('@') + 1);
},
set() {
expectType<Document<any, any, AutoTypedSchemaType> & AutoTypedSchemaType>(this);
expectType<Document<any, any, SchemaWithoutVirtual>>(this);
},
options: {}
}
Expand All @@ -118,5 +118,18 @@ function autoTypedVirtuals() {
const doc = new TestModel();
expectType<string>(doc.domain);

expectType<FlatRecord<AutoTypedSchemaType & VirtualsType >>({} as InferredDocType);
TestModel.findOne({}).then((doc) => {
if (doc) {
expectType<string>(doc.domain);
const json = doc.toJSON();
expectType<string>(json.domain);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. domain shouldn't be in json unless you have toJSON: { virtuals: true } in your schema or globally.

}
});

type TestDoc = InferSchemaType<typeof testSchema>;

const testDoc = { email: 'some email' } as TestDoc;
expectType<string>(testDoc.domain);

expectType<FlatRecord<AutoTypedSchemaType>>({} as InferredDocType);
}
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Expand Up @@ -38,7 +38,7 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;
type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'> & ObtainSchemaGeneric<TSchema, 'TVirtuals'>>;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down
6 changes: 3 additions & 3 deletions types/models.d.ts
Expand Up @@ -124,13 +124,13 @@ declare module 'mongoose' {
interface RemoveOptions extends SessionOption, Omit<mongodb.DeleteOptions, 'session'> {}

const Model: Model<any>;
interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}, TSchema = any> extends
interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, EnforcedVirtuals = {}, TSchema = any,
TVirtuals extends IfEquals<EnforcedVirtuals, {}, ObtainSchemaGeneric<TSchema, 'TVirtuals'>, EnforcedVirtuals> = IfEquals<EnforcedVirtuals, {}, ObtainSchemaGeneric<TSchema, 'TVirtuals'>, EnforcedVirtuals>> extends
NodeJS.EventEmitter,
AcceptsDiscriminator,
IndexManager,
SessionStarter {
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides,
IfEquals<TVirtuals, {}, ObtainSchemaGeneric<TSchema, 'TVirtuals'>, 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