Skip to content

fix: Type regressions in ascending/descending fields#2919

Open
swittk wants to merge 3 commits intoparse-community:alphafrom
swittk:fix-ascending-descending-types
Open

fix: Type regressions in ascending/descending fields#2919
swittk wants to merge 3 commits intoparse-community:alphafrom
swittk:fix-ascending-descending-types

Conversation

@swittk
Copy link
Contributor

@swittk swittk commented Feb 13, 2026

Pull Request

Issue

Typings for ascending, descending, addAscending, and addDescending in the current version are typed as ...string[], which misses the original object key checks which were possible in the original DefinitelyTyped typings.
This change allows for the autocompletion & key inference type checks to work once again (this includes base attributes such as objectId, createdAt, updatedAt too).
The comma-string keys (which are technically supported, JS-wise, but were not supported in the original DefinitelyTyped typings) are not included in this scope yet, but should be possible to add in a future PR by using modern features.

Approach

Used key inference in the same fashion as in the prior DefinitelyTyped typings.

Tasks

  • Add tests

Summary by CodeRabbit

  • Refactor
    • Improved type safety for query sorting and field selection to enhance compile-time validation and autocompletion. No runtime behavior changes.
  • New Features
    • Query APIs now accept text-score (for full-text ranking) and common metadata fields (creation/update timestamps and object IDs) in their type signatures for better developer ergonomics.

@parse-github-assistant
Copy link

🚀 Thanks for opening this pull request!

@parseplatformorg
Copy link
Contributor

parseplatformorg commented Feb 13, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai
Copy link

coderabbitai bot commented Feb 13, 2026

📝 Walkthrough

Walkthrough

Signatures for ParseQuery sorting and selection methods were narrowed to generics constrained to model attributes and base attributes; select and ascending/descending variants now allow the special '$score' key in types. Declarations and type-tests were updated accordingly.

Changes

Cohort / File(s) Summary
ParseQuery implementation
src/ParseQuery.ts
Tightened method signatures: ascending, addAscending, descending, addDescending use generic `K extends keyof T['attributes']
Type declarations
types/ParseQuery.d.ts
Matching signature updates in declarations: generics added to sort/select methods, constraints include `keyof T['attributes']
Type tests
types/tests.ts
Extended type tests to accept createdAt, updatedAt, objectId in sort methods and '$score' in select; added positive type assertions for resulting ParseQuery<MySubClass> instances.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • mtrezza
🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing TypeScript type regressions in ascending/descending field methods.
Description check ✅ Passed The PR description addresses the issue and approach, but the 'Approach' section lacks specific implementation details and the 'Tasks' section shows incomplete checklist items.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into alpha

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/ParseQuery.ts (1)

1566-1570: ⚠️ Potential issue | 🟠 Major

sortByTextScore will fail to compile with the new ascending constraint.

'$score' is not a member of keyof T['attributes'] | keyof BaseAttributes, so this.ascending('$score') on line 1567 will produce a type error after this change. The select call already works around this with as any, but ascending does not.

Proposed fix
   sortByTextScore() {
-    this.ascending('$score');
+    this.ascending('$score' as any);
     this.select(['$score'] as any);
     return this;
   }
🤖 Fix all issues with AI agents
In `@types/ParseQuery.d.ts`:
- Line 737: The new key constraint on
ascending/descending/addAscending/addDescending is too strict and breaks
sortByTextScore(), which calls this.ascending('$score'); relax the signatures in
types/ParseQuery.d.ts (the ascending, descending, addAscending, addDescending
declarations) to allow special string keys (either include | string or
explicitly include the literal '$score') in addition to keyof T['attributes'] |
keyof BaseAttributes so calls like this.ascending('$score') compile without
casts; update the declarations to accept (K | string | K[])[] or add an overload
that accepts string | string[] to preserve type safety for attribute keys while
supporting '$score'.

@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a46908c) to head (ef96ed2).

Additional details and impacted files
@@            Coverage Diff            @@
##             alpha     #2919   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         6235      6235           
  Branches      1493      1493           
=========================================
  Hits          6235      6235           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 13, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
types/ParseQuery.d.ts (1)

747-764: ⚠️ Potential issue | 🟡 Minor

'$score' is missing from addAscending, descending, and addDescending constraints.

ascending and select include '$score' in their generic constraint, but the other three sort methods do not. A user who writes query.addAscending('$score') or query.descending('$score') will get a type error, even though the runtime accepts it. If the omission is intentional (only the ascending('$score') pattern is officially supported), a brief JSDoc note on these methods would help; otherwise, add | '$score' for consistency.

Proposed fix for consistency
-    addAscending<K extends keyof T['attributes'] | keyof BaseAttributes>(...keys: (K | K[])[]): this;
+    addAscending<K extends keyof T['attributes'] | keyof BaseAttributes | '$score'>(...keys: (K | K[])[]): this;
...
-    descending<K extends keyof T['attributes'] | keyof BaseAttributes>(...keys: (K | K[])[]): this;
+    descending<K extends keyof T['attributes'] | keyof BaseAttributes | '$score'>(...keys: (K | K[])[]): this;
...
-    addDescending<K extends keyof T['attributes'] | keyof BaseAttributes>(...keys: (K | K[])[]): this;
+    addDescending<K extends keyof T['attributes'] | keyof BaseAttributes | '$score'>(...keys: (K | K[])[]): this;

Comment on lines -1568 to +1569
this.select(['$score'] as any);
this.select('$score');
Copy link
Member

@mtrezza mtrezza Feb 15, 2026

Choose a reason for hiding this comment

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

Could you give more context about why this changed from Array<string> to string? Parse.Query.select usually takes an array, but from the description I understand it also accepts a single string with comma separation. I think we should remove this type ambiguity and make select only accept an array of strings. If you agree we could pin the change for the next major release. We should then keep using the array type wherever possible because we'll have to revert to it.

Copy link
Contributor Author

@swittk swittk Feb 21, 2026

Choose a reason for hiding this comment

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

Sorry for the late reply; there isn't really any need for this change (it could be just ['$score'] the same as before), but there's really no real behavioural difference underneath I think; internally it just calls Array.isArray(key) check before, but then it does the key.join() immediately after + does a split(',') afterwards, so internally it supports all 3 types I mentioned (single array of keys, keys as string args, keys as string with comma splitting), and even likely supports a mix-and-match of all 3 types as multiple args due to how it's implemented in the addAscending and addDescending call sites.
The typings I think what people are definitely using (at least in my team) are:

  • .addAscending('key1', 'key2') (the variadic arguments version)
  • or the .addAscending(['key1', 'key2']) (the array args version)

I don't recall seeing the comma separated string thing documented, but it's definitely working and has been there a long time. I don't think we should remove this to be honest. I do have an idea for how to make the comma-separated typed multiple keys thing work strongly typed, but I was thinking of doing a separate PR for this since it seemed like a bit of TS magic. Just making keys typed the conventional way seemed straightforward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mtrezza sorry for the ping; do you have any suggestions to what I should edit?

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