Skip to content

Commit

Permalink
fix(types): indicate that Schema.prototype.discriminator() returns `t…
Browse files Browse the repository at this point in the history
…his`

Fix #12457
  • Loading branch information
vkarpov15 committed Oct 4, 2022
1 parent 140524c commit a15242d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
8 changes: 2 additions & 6 deletions test/types/schema.test.ts
Expand Up @@ -570,9 +570,7 @@ export type AutoTypedSchemaType = {
// discriminator
const eventSchema = new Schema<{ message: string }>({ message: String }, { discriminatorKey: 'kind' });
const batchSchema = new Schema<{ name: string }>({ name: String }, { discriminatorKey: 'kind' });
const discriminatedSchema = batchSchema.discriminator('event', eventSchema);

expectType<Schema<Omit<{ name: string }, 'message'> & { message: string }>>(discriminatedSchema);
batchSchema.discriminator('event', eventSchema);

// discriminator statics
const eventSchema2 = new Schema({ message: String }, { discriminatorKey: 'kind', statics: { static1: function() {
Expand All @@ -581,9 +579,7 @@ const eventSchema2 = new Schema({ message: String }, { discriminatorKey: 'kind',
const batchSchema2 = new Schema({ name: String }, { discriminatorKey: 'kind', statics: { static2: function() {
return 1;
} } });
const discriminatedSchema2 = batchSchema2.discriminator('event', eventSchema2);

expectAssignable<Schema<Omit<{ name: string }, 'message'> & { message: string }, Model<any>, {}, {}, {}, { static1(): number; static2(): number; }>>(discriminatedSchema2);
batchSchema2.discriminator('event', eventSchema2);

function gh11828() {
interface IUser {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -208,7 +208,7 @@ declare module 'mongoose' {
/** Returns a copy of this schema */
clone<T = this>(): T;

discriminator<DisSchema = Schema>(name: string, schema: DisSchema): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema>;
discriminator<DisSchema = Schema>(name: string, schema: DisSchema): this;

/** Returns a new schema that has the picked `paths` from this schema. */
pick<T = this>(paths: string[], options?: SchemaOptions): T;
Expand Down

0 comments on commit a15242d

Please sign in to comment.