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
Update document.d.ts set function params #13304
Conversation
/** * Alias for `set()`, used internally to avoid conflicts * * @param {String|Object} path path or object of key/vals to set * @param {Any} val the value to set * @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes * @param {Object} [options] optionally specify options that modify the behavior of the set * @param {Boolean} [options.merge=false] if true, setting a [nested path](/docs/subdocs.html#subdocuments-versus-nested-paths) will merge existing values rather than overwrite the whole object. So `doc.set('nested', { a: 1, b: 2 })` becomes `doc.set('nested.a', 1); doc.set('nested.b', 2);` * @return {Document} this * @method $set * @memberof Document * @instance * @api public */
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, maybe consider adding some type tests so that it does not get forgotten in the future
$set(path: string, val: any, type: any, options?: DocumentSetOptions): this; | ||
$set(path: string, val: any, options?: DocumentSetOptions): this; | ||
$set(value: any): this; | ||
$set(path: string | Record<string, any>, val: any, type: any, options?: DocumentSetOptions): this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite correct, Mongoose ignores val
if path
is an object. So I think the more correct approach would be to add an additional override to $set()
that looks like this:
$set(Record<string, any>, val?: null | undefined, type?: any, options?: DocumentSetOptions): this;
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite correct, Mongoose ignores val if path is an object.
if i understand it correctly, a additional overload would be needed like
$set(path: Record<string, any>, type: any, options?: DocumentSetOptions): this;
or is type
not just assigned from parameter 2 and still needs val: never
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I took a closer look and, in order to apply the changes I suggested, we'd also need to change the function signature for set()
, and I'd rather not do that for now. Given that this makes our types more consistent, I'm going to merge this as is.
Summary
Examples