Skip to content

Conversation

@TkDodo
Copy link
Collaborator

@TkDodo TkDodo commented Mar 29, 2021

This PR picks up where #1955 left off and infers the QueryKey of the QueryContext passed to the queryFn from the actual QueryKey

@vercel
Copy link

vercel bot commented Mar 29, 2021

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/tannerlinsley/react-query/FYbQgJGixQ14Y4boknDtGxedHgeG
✅ Preview: https://react-query-git-fork-tkdodo-feature-infer-query-key-t-2a2992.vercel.app

for more type safety and to make queryKeyHashFn generic as well
.fetchOptimistic(defaultedOptions)
.then(({ data }) => {
defaultedOptions.onSuccess?.(data)
defaultedOptions.onSuccess?.(data as TData)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@boschni I went a bit down the rabbithole with that last commit (395a509), but I think types are now more exact than before. here, for example, data is inferred to TData | undefined, which is correct, and it was any before. But onSuccess expects TData, which is why I added the type assertion. I think this is the only real change - everything else is just drilling generics :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice work! I didn't pursue typing the query key in v3 because it does add a lot of generics and I was not sure if people would actually use the query context. But if there is need for it then this looks good 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

can we merge it then? I'd like to fix types of initialData but I don't want to start until this huge type monster is merged :)

// Create query function context
const queryKey = ensureArray(this.queryKey)
const queryFnContext: QueryFunctionContext = {
const queryFnContext: QueryFunctionContext<unknown[]> = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I needed to use unknown[] here because after ensureArray, TQueryKey is no longer true, as that contains strings as well. I think it's not a big deal, but if you know how to make ensureArray return TQueryKey if the input is an array and [string] is not, we could improve this further.

Copy link
Member

@tannerlinsley tannerlinsley left a comment

Choose a reason for hiding this comment

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

This looks good to me, but I'd like @boschni's approval as well before merging.

@tannerlinsley tannerlinsley merged commit e42cbc3 into TanStack:master Mar 31, 2021
@tannerlinsley
Copy link
Member

🎉 This PR is included in version 3.13.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants