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

Add strictPopulate type to PopulateOptions #11544

Merged
merged 1 commit into from
Mar 21, 2022

Conversation

Kaljuk
Copy link
Contributor

@Kaljuk Kaljuk commented Mar 19, 2022

Added strictPopulate to PopulateOptions, otherwise given option can't be used in TS projects when enforcing types.

Summary

When using populate in mongoose 6, the types for populate options might've been overlooked. PopulateOptions interface is missing strictPopulate prop, so I added one, with description from the doc. Otherwise I can't use populate with given option.

Examples

await DocumentModel.findById('some_id')
  .populate({
    match: { somePath: { $ne: undefined } } 
    path: 'somePath',
    strictPopulate: false
  })

Previously this was not allowed in typescript dev environment due to enforcing of types, and a type error was thrown.
Now I added the respective type and all works.

For future reference
It seems that the options?: any seems to be unnecessary and wrongfully referring to options.options. But when we look at the current codebase with strictPopulate for example, then the value will be taken from options.strictPopulate, not options.options.strictPopulate.

TL;DR I'd suggest removing the options?: any from the PopulateOptions entirely and replace it with the specific options (i.e. strictPopulate, like in this PR)

Added strictPopulate to PopulateOptions, otherwise given option can't be used in TS projects when enforcing types.
types/index.d.ts Show resolved Hide resolved
@AbdelrahmanHafez
Copy link
Collaborator

Welcome, @Kaljuk 👋

Other than the missing test, LGTM.
I agree that having typed options is better than any, and I personally think that having them all in one place would be more accurate, either top-level or all in options.

Sadly we can't do that change now because it'll be backward breaking, we should at least add accurate typings to represent the API.

You're more than welcome to submit PRs improving the types.

@AbdelrahmanHafez AbdelrahmanHafez added this to the 6.2.10 milestone Mar 20, 2022
@AbdelrahmanHafez AbdelrahmanHafez added the typescript Types or Types-test related issue / Pull Request label Mar 20, 2022
@AbdelrahmanHafez AbdelrahmanHafez merged commit cda9c97 into Automattic:master Mar 21, 2022
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.

Thanks 👍

@vkarpov15 vkarpov15 modified the milestones: 6.2.10, 6.2.9, 6.2.8 Mar 23, 2022
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