Can't get `structuralSharing` function to build after upgrading to 5.x. #6525
Send to my agentDescribe the bug#
I had a structuralSharing function that worked in 4.36.1, but it no longer compiles with 5.13.4.
I've included a playground link below. Here's the minimal repro:
type Foo = { fingerprint: number }
const x: UseQueryOptions<Foo> = {
queryKey: ["foo"],
queryFn: () => ({ fingerprint: 1 }),
structuralSharing: (oldData: Foo | undefined, newData: Foo) =>
oldData?.fingerprint === newData.fingerprint ? oldData : newData,
}
In case it's interesting, I previously didn't need to annotate the types for function since structuralSharing used TData from QueryOptions rather than a free-standing, unrelated T.
Your minimal, reproducible example#
Steps to reproduce#
- Open the linked tiny repro playground.
- Notice the red squiggly.
Expected behavior#
No tsc errors.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
5.13.4
Tanstack Query adapter#
None
TanStack Query version#
v5.13.4
TypeScript version#
5.2.2
Additional context#
No response
TkDodo added labelsbug
TkDodo added labelstypes
TkDodo added labelshelp wanted
So I've been playing around with this and we can get it working if we change the queryOptions type structuralSharing property to:
structuralSharing?: boolean | StructuralSharingFunction<TData>And add
type StructuralSharingFunction<T> = (oldData: T | undefined, newData: T) => T;Then in QueryObserverOptions, we omit structuralSharing from QueryOptions and redeclare it in there as
structuralSharing?: boolean | StructuralSharingFunction<TData>This means that if select is set - it will correctly use the select return type, rather than the query function result type.
The problem is that it breaks a few tests, because in a few places we do this:
const options = queryOptions({ queryKey: ["key"], queryFn: () => Promise.resolve(1), }); const query = reactive( useQuery({ ...options, select: (data) => data > 1, }), );and structuralSharing will then have 2 incompatible types due to spreading. I'm wondering if this is actually a legit type of test though. I can't see why we'd need to support that.
I think you're on a good way, however, this is not quite what happens at runtime:
This means that if select is set - it will correctly use the select return type, rather than the query function result type.
We always run the result of the queryFn (TQueryFnData) through structuralSharing. Then, if select is specified, we run the result of select (TData) through it as well.
OK, so if a select function is supplied, oldData and newData can either be TQueryFnData or TData. Would that be correct?
Yes, but output needs to match input, so I think something along the lines of
StructuralSharingFunction<TQueryFnData> | StructuralSharingFunction<TData>Sorry, this has turned into an even bigger pain than I thought due to type inference when using the queryOptions function. If we use StructuralSharingFunction | StructuralSharingFunction then the structural sharing method can update the expected TData and TQueryFnData type.
Even setting it to unknown causes issues. I've got a very minimal example of the issues here so that you can see what I mean:
I'm unsure how to proceed.
hm, even
test3is technically wrong because if we supplyselect, we will callstructuralSharingonce withResult1(before select is applied, on the result of the queryFn) and then once more after select, with the result ofselect, soboolean.This would make the correct type for
structuralSharing:| ((oldData: Return1 | undefined, newData: Return1 | undefined): Return1) | ((oldData: boolean | undefined, newData: boolean | undefined): boolean)@charlotte-bone @TkDodo @tannerlinsley Can someone explain to me why changing the type definition of
structuralSharinginpackages/query-core/src/types.tswould not resolve this? So:structuralSharing?: boolean | ((oldData: TData | undefined, newData: TData) => TData)Instead of:
structuralSharing?: boolean | (<T>(oldData: T | undefined, newData: T) => T)@niels-bosman that's what we had before. But
structuralSharingcan actually receiveTDataorTQueryFnData- it's called once after the queryFn runs withTQueryFnData, and potentially once more afterselectwithTData.come to think about it, we're also using it on
combineofuseQueriesto structurally share that result. So I think the best and only fix is to remove the type argument and just always have it beunknown.TkDodo closed this
Sign in to join this conversation.
Yeah, the generic I made there is not working, sorry for that. Generally, the change is that
structuralSharingwill getunknownpassed, because it will also be called afterselect. I wanted to give an option to bypass this with the generic, but it doesn't work like that.If you want to take a look at fixing it, please do. Otherwise, I'll just hard-code the type to
unknown