Skip to content

fix(pinia-orm): accept eager load callback for optional relations#2030

Merged
CodeDredd merged 1 commit into
mainfrom
fix/with-callback-optional-relations
Jul 19, 2026
Merged

fix(pinia-orm): accept eager load callback for optional relations#2030
CodeDredd merged 1 commit into
mainfrom
fix/with-callback-optional-relations

Conversation

@CodeDredd

@CodeDredd CodeDredd commented Jul 19, 2026

Copy link
Copy Markdown
Owner

Bug

When a relation is declared as an optional property —

@HasMany(() => Review, 'shipment_id') declare reviews?: Review[]

— the property type includes undefined, so WithKeys<M> skipped the key and the callback conditional in with() / whereHas() / whereDoesntHave() fell through to () => void / never. Passing a query constraint callback then failed to compile (#1999).

Fix

  • WithKeys<T> now strips optionality (-?) and checks NonNullable<T[P]>
  • the callback conditionals additionally accept undefined in the union (M[T] extends Model | Model[] | null | undefined ? …)
  • same treatment for the exported UltimateKeys helper

Type-only change, no runtime behavior touched.

Tests

New test using an optional comments?: Comment[] relation with a with() constraint callback — produces 2 tsc errors before the fix, compiles clean after (verified with tsc --noEmit; no new errors elsewhere). Full suite green (402 tests).

fixes #1999

Relations declared with TypeScript optional properties (posts?: Post[])
were not covered by WithKeys and the eager load callback conditionals,
because the property type includes undefined. The callback of with(),
whereHas() etc. then resolved to '() => void'/'never' and passing a
query constraint produced a compile error.

fixes #1999
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.43%. Comparing base (7a32aec) to head (a5812ac).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2030   +/-   ##
=======================================
  Coverage   99.43%   99.43%           
=======================================
  Files          82       82           
  Lines        3022     3022           
  Branches      530      530           
=======================================
  Hits         3005     3005           
  Misses         14       14           
  Partials        3        3           

☔ View full report in Codecov by Harness.
📢 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.

@CodeDredd
CodeDredd merged commit 4cf2d7e into main Jul 19, 2026
13 checks passed
@CodeDredd
CodeDredd deleted the fix/with-callback-optional-relations branch July 19, 2026 21:37
@github-actions github-actions Bot mentioned this pull request Jul 20, 2026
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.

Typescript signature of with() does not seem correct when function is provided as second argument

2 participants