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] How to set the model type of discriminator? #10421

Closed
lutherman opened this issue Jul 5, 2021 · 4 comments · Fixed by #10452
Closed

[Types] How to set the model type of discriminator? #10421

lutherman opened this issue Jul 5, 2021 · 4 comments · Fixed by #10452
Assignees
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@lutherman
Copy link

Hi,

I tried to set Model type for discriminator, but got the type error.

Codes:

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, // <-- error here
  Model<IClickedLinkEvent>
>('ClickedLink', new Schema({ url: String }, options));

And the error message:

Type 'IClickedLinkEvent' does not satisfy the constraint 'Document<any, any, any>'.
  Type 'IClickedLinkEvent' is missing the following properties from type 'Document<any, any, any>': $getAllSubdocs, $ignore, $isDefault, $isDeleted, and 48 more.ts(2344)

If my syntax is not correct, would you mind telling me the correct answer?


nodejs: 12.19.0
mongodb: 4.2.7
mongoose: 5.13.2

@lutherman lutherman changed the title [Types] How do i set the Model type of discriminator? [Types] How do i set the model type of discriminator? Jul 5, 2021
@lutherman lutherman changed the title [Types] How do i set the model type of discriminator? [Types] How to set the model type of discriminator? Jul 6, 2021
@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jul 6, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Jul 6, 2021

import * as mongoose from 'mongoose';

const options = { discriminatorKey: 'kind' };
interface IEvent extends mongoose.Document{
  time: Date;
  kind: string;
}

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

interface IClickedLinkEvent extends IEvent {
  url: string;
}

const ClickedLinkEvent = Event.discriminator<
  IClickedLinkEvent,
  mongoose.Model<IClickedLinkEvent>
>('ClickedLink', new mongoose.Schema({ url: String }, options));

@lutherman
Copy link
Author

lutherman commented Jul 7, 2021

Do you mean the interface must extend mongoose.Document?

But mongoose official document (link) recommends not extend:
... This approach works, but we recommend your document interface not extend Document. Using extends Document makes it difficult for Mongoose to infer which properties are present on query filters, lean documents, and other cases. ...

Therefore, i changed all interfaces back into not extend. It took me several days.

Which one should i follow?

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jul 7, 2021
@vkarpov15 vkarpov15 added this to the 5.13.3 milestone Jul 7, 2021
@vkarpov15
Copy link
Collaborator

@lutherman you're correct, we recommend that your document interface doesn't extends Document. We'll look into this, sorry for the inconvenience.

@lutherman
Copy link
Author

@vkarpov15 It’s fine. Thank you for following up.

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.

3 participants