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

fix(types): remove discriminator type requirement #10452

Merged
merged 1 commit into from
Jul 16, 2021

Conversation

DouglasGabr
Copy link
Contributor

@DouglasGabr DouglasGabr commented Jul 15, 2021

Summary

As described in the issue #10421, the .discriminator function is yielding an error when trying to use an interface that doesn't extends Document.
Since Model enforces Document when needed, the discriminator type parameter doesn't need to extend it.

Fixes #10421

Examples

import { Model, model, Schema } from 'mongoose';

const options = { discriminatorKey: 'kind' };
interface IEvent {
  time: Date;
  kind: string;
}

const eventSchema = new Schema<IEvent, Model<IEvent>>({ time: Date }, options);
const Event = model<IEvent, Model<IEvent>>('Event', eventSchema);

interface IClickedLinkEvent extends IEvent {
  url: string;
}

// ClickedLinkEvent is a special type of Event that has
// a URL.
const ClickedLinkEvent = Event.discriminator<
  IClickedLinkEvent, // <-- no more error here
  Model<IClickedLinkEvent>
>('ClickedLink', new Schema({ url: String }, options));

Since Model enforces Document when needed,
the discriminator type parameter don't need to extend it
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@vkarpov15 vkarpov15 merged commit ed1bffb into Automattic:master Jul 16, 2021
@vkarpov15 vkarpov15 added this to the 5.13.3 milestone Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Types] How to set the model type of discriminator?
2 participants