TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
40.54k stars 2.73k forks source link

Function passed to `notifyOnChangeProps` cannot return `undefined` #7426

Open winghouchan opened 1 month ago

winghouchan commented 1 month ago

Describe the bug

Background

notifyOnChangeProps is a query option that determines if the query should notify listeners if query properties change. It has the following type: https://github.com/TanStack/query/blob/1d60d44ad851472d37aaa3eef22ada07e5245989/packages/query-core/src/types.ts#L144-L148

Depending on the value set, it has the following behaviours:

It can also be set with a function with the signature () => string[] | 'all'. The query will run the function and follow the behaviour listed above that matches the returned value.

With the available non-function options of undefined, string[], or 'all', it follows that the function should also be able to return undefined. However, the current type signature does not allow it.

Real world example

One real world example of the function passed to notifyOnChangeProps returning undefined comes from React Query's documentation itself, for disabling re-renders on out of focus screens on React Native. See this TypeScript Playground for a reproduction.

Impact

Allowing the function passed to notifyOnChangeProps to return undefined should not have any negative impact. At runtime, the query observer sets the value of notifyOnChangeProps or the value of the call to notifyOnChangeProps if it was a function to the same variable. This means if undefined is a valid value for notifyOnChangeProps then a call to notifyOnChangeProps should also be allowed to be undefined. See: https://github.com/TanStack/query/blob/1d60d44ad851472d37aaa3eef22ada07e5245989/packages/query-core/src/queryObserver.ts#L612-L616

Your minimal, reproducible example

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAOQACMAhgHaoMDGA1gPRTr2swBaAI458VAFDi0WUXgAUCcXGVwRuPADFGALjj1UeRqzhyAlHAC8APjgwoOADRKVa-AGl0eXQG0qrvB54VAC6TipwjBAwwCR4APKMAMIAFkwA5ugACuRgqLpmljbIjAAm6CTAjOglToSmQA

Steps to reproduce

Set notifyOnChangeProps query option to a function that (could) return undefined.

Expected behavior

No type error should occur.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

N/A

Tanstack Query adapter

None

TanStack Query version

5.29.2

TypeScript version

5.4.5

Additional context

No response

winghouchan commented 1 month ago

The resolution should be to just update https://github.com/TanStack/query/blob/1d60d44ad851472d37aaa3eef22ada07e5245989/packages/query-core/src/types.ts#L147

to

| (() => Array<keyof InfiniteQueryObserverResult> | 'all' | undefined) 

Happy to open a PR if my understanding described above is correct.

TkDodo commented 1 month ago

Yes you are right. It will already work at runtime because if undefined is returned from the function, we treat it the same as if the prop itself is undefined:

https://github.com/TanStack/query/blob/4d5cc6679c1212af8a9d611fa0f535144feb2a19/packages/query-core/src/queryObserver.ts#L625

Happy to accept a PR to adapt the types 👍