Skip to content

Commit

Permalink
Merge pull request #11898 from GCastilho/fix/discriminator-type-inher…
Browse files Browse the repository at this point in the history
…itance

fix: Discriminator generic type not being passed to schema
  • Loading branch information
vkarpov15 committed Jun 7, 2022
2 parents 58746bc + 28e1c78 commit 0612349
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions types/models.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
declare module 'mongoose' {
import mongodb = require('mongodb');

export interface AcceptsDiscriminator {
export interface AcceptsDiscriminator<B> {
/** Adds a discriminator type. */
discriminator<D>(name: string | number, schema: Schema, value?: string | number | ObjectId): Model<D>;
discriminator<D>(name: string | number, schema: Schema<D>, value?: string | number | ObjectId): Model<Omit<B, keyof D> & D>;
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string | number | ObjectId): U;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ declare module 'mongoose' {
const Model: Model<any>;
interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}> extends
NodeJS.EventEmitter,
AcceptsDiscriminator,
AcceptsDiscriminator<T>,
IndexManager,
SessionStarter {
new <DocType = AnyKeys<T> & AnyObject>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides, TVirtuals>;
Expand Down Expand Up @@ -416,4 +416,4 @@ declare module 'mongoose' {
where<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(obj: object): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
where<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
}
}
}
18 changes: 9 additions & 9 deletions types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,17 @@ declare module 'mongoose' {

namespace Schema {
namespace Types {
class Array extends SchemaType implements AcceptsDiscriminator {
class Array<B = any> extends SchemaType<B> implements AcceptsDiscriminator<B> {
/** This schema type's name, to defend against minifiers that mangle function names. */
static schemaName: 'Array';

static options: { castNonArrays: boolean; };

discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<D>(name: string | number, schema: Schema<D>, value?: string | number | ObjectId): Model<Omit<B, keyof D> & D>;

/** The schematype embedded in this array */
caster?: SchemaType;
caster?: SchemaType<B>;

/**
* Adds an enum validator if this is an array of strings or numbers. Equivalent to
Expand Down Expand Up @@ -329,17 +329,17 @@ declare module 'mongoose' {
static schemaName: 'Decimal128';
}

class DocumentArray extends SchemaType implements AcceptsDiscriminator {
class DocumentArray<B = any> extends SchemaType<B> implements AcceptsDiscriminator<B> {
/** This schema type's name, to defend against minifiers that mangle function names. */
static schemaName: 'DocumentArray';

static options: { castNonArrays: boolean; };

discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<D>(name: string | number, schema: Schema<D>, value?: string | number | ObjectId): Model<Omit<B, keyof D> & D>;
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;

/** The schema used for documents in this array */
schema: Schema;
schema: Schema<B>;

/** The constructor used for subdocuments in this array */
caster?: typeof Types.Subdocument;
Expand Down Expand Up @@ -377,15 +377,15 @@ declare module 'mongoose' {
auto(turnOn: boolean): this;
}

class Subdocument extends SchemaType implements AcceptsDiscriminator {
class Subdocument<B = any> extends SchemaType<B> implements AcceptsDiscriminator<B> {
/** This schema type's name, to defend against minifiers that mangle function names. */
static schemaName: string;

/** The document's schema */
schema: Schema;
schema: Schema<B>;

discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<D>(name: string | number, schema: Schema<D>, value?: string | number | ObjectId): Model<Omit<B, keyof D> & D>;
}

class String extends SchemaType {
Expand Down

0 comments on commit 0612349

Please sign in to comment.