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 type errors when using fully-typed inputs like QueryFilters<Data, Error>, and test all QueryClient methods to detect similar issues #8375

Merged
merged 21 commits into from
Dec 3, 2024

Conversation

Nick-Lucas
Copy link
Contributor

@Nick-Lucas Nick-Lucas commented Nov 30, 2024

After adding type parameters to QueryFilters, some methods are proving to be incompatible with fully-typed usage

This PR adds a complete suite of fully-typed calls to all QueryClient methods, and method overloads which accept these inputs.

The core issue is that while TFoo & { bar: string } can be assigned to TFoo, if there are any callbacks (ie. predicate) that pass TFoo back, typescript will attempts to assign TFoo to TFoo & { bar: string }, and the default of unknown has the same problem, this is a limitation of subtyping in typescript. To get around this we make the QueryFilter type a generic parameter which can adopt the type of the method argument

image

This is impacting the new tRPC client which the QueryFilter typings were originally added for, in two ways:

image

Firstly the error type needs declaring because tRPC doesn't expect an Error to serialise across the wire, second once declaring this the data type shows up as incompatible because the QueryFilters type is ignored. This PR adds generic type parameters for QueryFilters to improve both situations

Copy link

nx-cloud bot commented Nov 30, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit bf7f163. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Nov 30, 2024

Open in Stackblitz

More templates

@tanstack/angular-query-devtools-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@8375

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@8375

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@8375

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@8375

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@8375

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@8375

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@8375

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@8375

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@8375

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@8375

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@8375

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@8375

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@8375

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@8375

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@8375

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@8375

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@8375

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@8375

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@8375

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@8375

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@8375

commit: bf7f163

@Nick-Lucas Nick-Lucas force-pushed the trpc-error-compatibility branch from 75dfaba to 26154ca Compare November 30, 2024 17:07
@Nick-Lucas Nick-Lucas changed the title Fix type errors when using QueryFilters with defined Data and Error types Fix type errors when using QueryFilters with defined Data and Error types, and test all QueryClient methods to detect similar issues Nov 30, 2024
@Nick-Lucas Nick-Lucas changed the title Fix type errors when using QueryFilters with defined Data and Error types, and test all QueryClient methods to detect similar issues Fix type errors when using fully-typed inputs like QueryFilters<Data, Error>, and test all QueryClient methods to detect similar issues Nov 30, 2024
@Nick-Lucas Nick-Lucas marked this pull request as ready for review November 30, 2024 18:50
@Nick-Lucas Nick-Lucas marked this pull request as draft November 30, 2024 19:31
@Nick-Lucas Nick-Lucas force-pushed the trpc-error-compatibility branch from 90f6a5d to 8804138 Compare December 1, 2024 11:38
Copy link

codecov bot commented Dec 1, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Project coverage is 62.91%. Comparing base (d9d2728) to head (bf7f163).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #8375       +/-   ##
===========================================
+ Coverage   46.12%   62.91%   +16.79%     
===========================================
  Files         200      136       -64     
  Lines        7504     4797     -2707     
  Branches     1713     1344      -369     
===========================================
- Hits         3461     3018      -443     
+ Misses       3668     1539     -2129     
+ Partials      375      240      -135     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental 87.13% <ø> (ø)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister 43.85% <ø> (ø)
@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core 94.57% <88.88%> (+0.05%) ⬆️
@tanstack/query-devtools 4.78% <ø> (ø)
@tanstack/query-persist-client-core 57.73% <ø> (ø)
@tanstack/query-sync-storage-persister 84.61% <ø> (ø)
@tanstack/react-query 95.54% <ø> (ø)
@tanstack/react-query-devtools 10.00% <ø> (ø)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (ø)
@tanstack/solid-query 78.20% <ø> (ø)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client 100.00% <ø> (ø)
@tanstack/svelte-query 87.33% <ø> (ø)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client 100.00% <ø> (ø)
@tanstack/vue-query 71.45% <ø> (ø)
@tanstack/vue-query-devtools ∅ <ø> (∅)

@Nick-Lucas Nick-Lucas marked this pull request as ready for review December 1, 2024 11:56
@Nick-Lucas
Copy link
Contributor Author

Nick-Lucas commented Dec 1, 2024

Hey @TkDodo I don't want to rush this one in as this changeset is wide-reaching. I experienced quite a few potential breaking changes I had to work around while developing this, so want to be really sure I'm not going to cause problems.

The described issues on 5.62.0 should not impact current usage of RQ, but if anybody wants to use the QueryFilters<...> form then they'll quickly find they can't call many of the QueryClient methods with it. This PR addresses that

@@ -36,9 +35,7 @@ export interface QueryFilters<
/**
* Include queries matching this query key
*/
queryKey?: unknown extends TQueryFnData
? QueryKey
: QueryKey & DataTag<unknown, TQueryFnData, TError>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the wrong idea, any fully-typed usage will define the TQueryKey and pass it in, and this approach was proving unreliable when I added richer tests.

Nick-Lucas and others added 2 commits December 2, 2024 20:34
…h supports all cases

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@Nick-Lucas
Copy link
Contributor Author

Nick-Lucas commented Dec 2, 2024

Okay simplified the changeset quite a lot now, instead of adding overloads it's looking a lot more like it looks on other methods which took generics, and the new tests & existing builds seem happy

@TkDodo TkDodo merged commit 175757a into TanStack:main Dec 3, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants