From ede6afa076a96dfb1509412fb6c4af674e9f5b3e Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 22 Sep 2022 11:20:26 +0200 Subject: [PATCH 1/3] fix(types): update "DiscriminatorSchema" to also transfer "TStaticMethods" also renames some type generics to be better distinguishable --- types/index.d.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 60cac795520..d43a6186f0f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -150,9 +150,10 @@ declare module 'mongoose' { : M : M; - export type DiscriminatorSchema = T extends Schema - ? Schema & T1, DiscriminatorModel, T3 | TInstanceMethods, T4 | TQueryHelpers, T5 | TVirtuals> - : Schema; + export type DiscriminatorSchema = + DisSchema extends Schema + ? Schema & DisSchemaEDocType, DiscriminatorModel, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics | TStaticMethods> + : Schema; type QueryResultType = T extends Query ? ResultType : never; @@ -195,7 +196,7 @@ declare module 'mongoose' { /** Returns a copy of this schema */ clone(): T; - discriminator(name: string, schema: T): DiscriminatorSchema; + discriminator(name: string, schema: DisSchema): DiscriminatorSchema; /** Returns a new schema that has the picked `paths` from this schema. */ pick(paths: string[], options?: SchemaOptions): T; From fe9b3e6d36d77b37b50624001f265a21a0fd9f34 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 22 Sep 2022 13:21:20 +0200 Subject: [PATCH 2/3] fix(types): combine static methods in "DiscriminatorSchema" --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index d43a6186f0f..6a41bfb2875 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -152,7 +152,7 @@ declare module 'mongoose' { export type DiscriminatorSchema = DisSchema extends Schema - ? Schema & DisSchemaEDocType, DiscriminatorModel, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics | TStaticMethods> + ? Schema & DisSchemaEDocType, DiscriminatorModel, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics & TStaticMethods> : Schema; type QueryResultType = T extends Query ? ResultType : never; From d9fd5b90d8e2e4292a4f48c50c9569459b4b0e65 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 22 Sep 2022 13:22:10 +0200 Subject: [PATCH 3/3] test(types-schema): add test for "schema.discriminator" with combining statics --- test/types/schema.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index c65b3bfdd9c..d992f356166 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -564,6 +564,17 @@ const discriminatedSchema = batchSchema.discriminator('event', eventSchema); expectType & { message: string }>>(discriminatedSchema); +// discriminator statics +const eventSchema2 = new Schema({ message: String }, { discriminatorKey: 'kind', statics: { static1: function() { + return 0; +} } }); +const batchSchema2 = new Schema({ name: String }, { discriminatorKey: 'kind', statics: { static2: function() { + return 1; +} } }); +const discriminatedSchema2 = batchSchema2.discriminator('event', eventSchema2); + +expectAssignable & { message: string }, Model, {}, {}, {}, { static1(): number; static2(): number; }>>(discriminatedSchema2); + function gh11828() { interface IUser { name: string;