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

[Types] Typing for Schema.discriminator does not include options parameter #15244

Closed
2 tasks done
JstnMcBrd opened this issue Feb 7, 2025 · 1 comment · Fixed by #15249
Closed
2 tasks done

[Types] Typing for Schema.discriminator does not include options parameter #15244

JstnMcBrd opened this issue Feb 7, 2025 · 1 comment · Fixed by #15249
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@JstnMcBrd
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.10.0

Node.js version

22.12.0

MongoDB server version

6.13.0

Typescript version (if applicable)

5.7.3

Description

The types for Schema.discriminator only allow 2 parameters (name and schema):

// https://github.com/Automattic/mongoose/blob/master/types/index.d.ts#L300
discriminator<DisSchema = Schema>(name: string | number, schema: DisSchema): this;

But in the actual code, it is clear that the function accepts an additional parameter (options):

// https://github.com/Automattic/mongoose/blob/master/lib/schema.js#L639
Schema.prototype.discriminator = function(name, schema, options) {
  this._applyDiscriminators = this._applyDiscriminators || new Map();
  this._applyDiscriminators.set(name, { schema, options });

  return this;
};

This discrepancy in the types makes it so users cannot specify different name and value without typecasting:

const schema = new Schema({});
(schema as any).discriminator("Name", new Schema({}), "value");

(Which incidentally, is the best workaround for #7422)

Steps to Reproduce

Try to create a discriminator on a Schema with a different name and value.

const schema = new Schema({});
schema.discriminator("Name", new Schema({}), "value"); // Type error

Expected Behavior

Types should reflect actual code.

const schema = new Schema({});
schema.discriminator("Name", new Schema({}), "value"); // No type error
@vkarpov15
Copy link
Collaborator

Fix is in #15249. Worth mentioning that it looks like Schema.prototype.discriminator() only supports DiscriminatorOptions as 3rd param, so schema.discriminator("Name", new Schema({}), "value") won't work, but schema.discriminator("Name", new Schema({}), { value: "value" }) will

@vkarpov15 vkarpov15 added this to the 8.10.1 milestone Feb 10, 2025
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Feb 10, 2025
vkarpov15 added a commit that referenced this issue Feb 12, 2025
types(schema): support `options` parameter to `Schema.prototype.discriminator()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants