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

fix(typescript): aggregate.sort() accepts a string but also { field: 'asc'|'ascending'|'desc'|'descending' } #11479

Merged
merged 3 commits into from
Mar 5, 2022
Merged

Conversation

simonbrunel
Copy link
Contributor

@simonbrunel simonbrunel commented Mar 4, 2022

When using TypeScript, calling model.aggregate().sort("-name") generates the following error:

Argument of type 'string' is not assignable to parameter of type 'Record<string, 1 | -1 | { $meta: "textScore"; }>'

This PR also adds support for mongoose sort keywords: asc, ascending, desc and descending (according to the current implementation and the mongoose docs).

Fix `Argument of type 'string' is not assignable to parameter of
type 'Record<string, 1 | -1 | { $meta: "textScore"; }>'` when
calling `model.aggregate().sort("-name")`. According to the current
implementation (and per the mongoose docs), `aggregate().sort()`
also accepts a string.
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case verifying the type under test/typescript to avoid this being changed some time in the future.

@AbdelrahmanHafez
Copy link
Collaborator

Other than the test case, LGTM.
Thanks 👍

@simonbrunel
Copy link
Contributor Author

simonbrunel commented Mar 5, 2022

Please add a test case verifying the type under test/typescript to avoid this being changed some time in the future.

+1000, I thought there was no test for types (I expected to find them under types/test and missed the ones in test/types).

Since there was no test for aggregate.sort(), I added them for all args supported by the current implementation. Note that asc, ascending, desc, descending was also not supported by the current type definition.

@simonbrunel simonbrunel changed the title fix(typescript): aggregate.sort() also accepts a string fix(typescript): aggregate.sort() also accepts a string but also { field: 'asc'|'ascending'|'desc'|'descending' } Mar 5, 2022
@simonbrunel simonbrunel changed the title fix(typescript): aggregate.sort() also accepts a string but also { field: 'asc'|'ascending'|'desc'|'descending' } fix(typescript): aggregate.sort() accepts a string but also { field: 'asc'|'ascending'|'desc'|'descending' } Mar 5, 2022
@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 5, 2022

Yeah, it is nice to see the typings tests

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks 👍

@AbdelrahmanHafez AbdelrahmanHafez merged commit a7a41b5 into Automattic:master Mar 5, 2022
@simonbrunel simonbrunel deleted the fix/typescript-aggregate-sort branch March 5, 2022 19:00
@AbdelrahmanHafez AbdelrahmanHafez added this to the 6.2.5 milestone Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants