-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
UpdateQuery generic type makes no sense on a schema with virtual getter and setter #10689
Comments
Our TypeScript docs don't recommend passing in an object that extends We unfortunately don't have an ideal solution for virtuals right now, but the workaround is to pass a separate interface containing virtuals as the import { Schema, Model, model } from 'mongoose';
interface SampleInput {
name: {
firstName: string;
lastName: string;
}
}
interface SampleInputVirtuals {
name: {
fullName: string;
}
}
type SampleModel = Model<SampleInput, {}, SampleInputVirtuals>; // <-- add virtuals here...
const schema = new Schema<SampleInput, SampleModel, SampleInputVirtuals>({ // <-- and here
name: {
firstName: String,
lastName: String
}
});
schema.virtual('name.fullName').get(function() {
return `${this.name.firstName} ${this.name.lastName}`;
});
const Sample = model<SampleInput, SampleModel>('Sample', schema);
const doc = new Sample({ name: { firstName: 'Jean-Luc', lastName: 'Picard' } });
doc.name.firstName.toUpperCase();
doc.name.lastName.toUpperCase();
doc.name.fullName.toUpperCase(); In v6.0.7 we're adding a type SampleModel = Model<SampleInput, {}, {}, SampleInputVirtuals>; // <-- add virtuals as a separate generic so they don't conflict with methods |
Thank you for your suggestion @vkarpov15 however I think there's a mistake in your code and in the docs too actually:
When I look at the type definition for Schema I find this: class Schema<
DocType = Document,
M extends Model<DocType, any, any> = Model<any, any, any>,
SchemaDefinitionType = undefined,
TInstanceMethods = ExtractMethods<M>
> //... It has 4 generic parameters, with the 3rd being
This is wrong. The 3rd generic isn't The reason why your example works (erroneously) is because there's no need to add |
Should I open a new issue for this? |
@DaDoBaag that looks like the syntax from Mongoose 5.x, my example assumes Mongoose 6.x. |
Oh I didn't realize that some typings changed with version 6, is there an overview of what changed? I don't find it in the changelog. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
UpdateQuery requires a Document interface generic but this doesn't work when your document has a virtual with getter and setter. If a virtual has both getter and setter I want to only allow the user to update either the get properties or the set property but the required document interface includes both.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
UpdateQuery integrates getters and setters in its generic type so it doesn't allow for both to be set at the same time.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node v14.16.1
mongoose 5.13.3
mongodb 5.0.1
The text was updated successfully, but these errors were encountered: