query icon indicating copy to clipboard operation
query copied to clipboard

Rename query data generics from TData to TQueryFnData for consistency

Open mattbrandlysonos opened this issue 3 years ago β€’ 5 comments

Hey! Thank you for react-query. It makes my job a lot easier.

Recently we ran into some confusion about the expected return value from getQueryData. The existing return type is called TData:

https://github.com/tannerlinsley/react-query/blob/fc4671f0731d1dd757f9fd037fd8e6755b3393d1/src/core/queryClient.ts#L105-L110

However, looking at the type for UseQueryOptions, TData refers to the value after the select function has been applied. The type that's returned from getQueryData is called TQueryFnData in this context.

https://github.com/tannerlinsley/react-query/blob/fc4671f0731d1dd757f9fd037fd8e6755b3393d1/src/react/types.ts#L26-L31

This PR presents no changes in functionality, but it aligns the generics names between UseQueryOptions and getQueryData! Now when working with UseQueryOptions, the names will help indicate the value returned from getQueryData.

To illustrate further, you might write code like this if you're expecting getQueryData to return TData, but the types here are incorrect!

type QueryOptions = UseQueryOptions<A, B, C>
type ExtractTData<P> = P extends UseQueryOptions<any, any, infer TData> ? TData : never;
type TData = ExtractTData<QueryOptions>

// ...

// `value` is not actually of type `TData`!
const value = queryClient.getQueryData<TData>(queryKey)

mattbrandlysonos avatar Mar 29 '22 16:03 mattbrandlysonos

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

πŸ” Inspect: https://vercel.com/tanstack/react-query/CoB3uuo9f9eoc3nR1DHbi4g4jykr
βœ… Preview: https://react-query-git-fork-mattbrandlysonos-brandly-patch-1-tanstack.vercel.app

vercel[bot] avatar Mar 29 '22 16:03 vercel[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 258efb462d958421f876a279cfd481a4b32ced92:

Sandbox Source
tannerlinsley/react-query: basic Configuration
tannerlinsley/react-query: basic-typescript Configuration

codesandbox-ci[bot] avatar Mar 29 '22 16:03 codesandbox-ci[bot]

yeah, I think I'm fine with the change :)

TkDodo avatar Apr 09 '22 13:04 TkDodo

Codecov Report

Merging #3451 (258efb4) into master (ba09e16) will not change coverage. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3451   +/-   ##
=======================================
  Coverage   96.41%   96.41%           
=======================================
  Files          45       45           
  Lines        2286     2286           
  Branches      642      642           
=======================================
  Hits         2204     2204           
  Misses         79       79           
  Partials        3        3           
Impacted Files Coverage Ξ”
src/core/queryClient.ts 100.00% <100.00%> (ΓΈ)

Continue to review full report at Codecov.

Legend - Click here to learn more Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data Powered by Codecov. Last update ba09e16...258efb4. Read the comment docs.

codecov[bot] avatar Apr 09 '22 13:04 codecov[bot]

Any updates regarding my comments here? Also, there are conflicts now.

TkDodo avatar Sep 04 '22 18:09 TkDodo

FYI, I will close stale PRs somewhen next week. Please either address the open comments / resolve the merge conflicts or this PR will be closed. Thanks πŸ™

TkDodo avatar Sep 23 '22 14:09 TkDodo

If you end up closing this I can take the current work and resolve your comments.

SammRosenthal avatar Sep 25 '22 23:09 SammRosenthal

@SammRosenthal yes please go ahead and create a new PR, thank you πŸ™

TkDodo avatar Sep 26 '22 04:09 TkDodo