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

Update document.d.ts set function params #13304

Merged
merged 1 commit into from May 8, 2023

Conversation

jeffersonlipsky
Copy link
Contributor

@jeffersonlipsky jeffersonlipsky commented Apr 20, 2023

/**
 * 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 */

Summary

Examples

/**
 * 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
 */
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Apr 22, 2023
Copy link
Collaborator

@hasezoey hasezoey left a 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;
Copy link
Collaborator

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?

Copy link
Collaborator

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?

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.

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.

@vkarpov15 vkarpov15 added this to the 7.1.1 milestone May 8, 2023
@vkarpov15 vkarpov15 merged commit c0b492c into Automattic:master May 8, 2023
3 checks passed
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 this pull request may close these issues.

None yet

3 participants