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
TypeScript: Property 'discriminator' does not exist on type 'SchemaType'. #10435
Comments
There's a couple of Mongoose issues here:
|
I tried a few workarounds, including using a Here's the workaround that we'll support as of v5.13.3: eventSchema.path<Schema.Types.Embedded>('props').discriminator('Clicked', clickedEventSchema);
eventSchema.path<Schema.Types.Embedded>('props').discriminator('Purchased', purchasedEventSchema); |
Mongoose 7.2.1. Is the best approach now: eventSchema.path<Schema.Types.Subdocument>('props').discriminator('Clicked', clickedEventSchema); ? |
@AaronPorts yeah that is the recommended workaround. The issue is that Mongoose's SchemaType base class does not have a |
This worked for me also, thanks for the answers. Are there any plans to have |
This will be a needed workaround for the foreseeable future. I'll add docs. |
docs: add brief note on TypeScript generic usage for embedded discriminator `path()` calls
Do you want to request a feature or report a bug?
bug
What is the current behavior?
When adding a discriminator for a single nested subdocuments (per the example in the blog post here: https://thecodebarbarian.com/mongoose-4.12-single-embedded-discriminators.html), TypeScript doesn't recognize the .discriminator method, reporting that it doesn't exist on type "SchemaType".
The code still works properly if the errors are suppressed:
If the current behavior is a bug, please provide the steps to reproduce.
My tsconfig.json:
What is the expected behavior?
TypeScript sees .discriminator method exists when called on instance of SchemaType returned by .path method, when path resolves to a single embedded subdocument
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v16.3.0
Mongoose: v5.13.2
MongoDB NPM package: v3.6.8 (from Mongoose dependency)
MongoDB server: v3.4.18
TypeScript: v4.3.5
The text was updated successfully, but these errors were encountered: