Skip to content

Commit dc4f24d

Browse files
committed
fix(types): widen types for structuralSharing
fixes #5806
1 parent 79b513c commit dc4f24d

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

docs/react/reference/useQuery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const {
155155
- If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `pending` state.
156156
- `placeholderData` is **not persisted** to the cache
157157
- If you provide a function for `placeholderData`, as a first argument you will receive previously watched query data if available, and the second argument will be the complete previousQuery instance.
158-
- `structuralSharing: boolean | ((oldData: TData | undefined, newData: TData) => TData)`
158+
- `structuralSharing: boolean | (<T>(oldData: T | undefined, newData: T) => T)`
159159
- Optional
160160
- Defaults to `true`
161161
- If set to `false`, structural sharing between query results will be disabled.

packages/query-core/src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ export interface QueryOptions<
152152
* Set this to a function which accepts the old and new data and returns resolved data of the same type to implement custom structural sharing logic.
153153
* Defaults to `true`.
154154
*/
155-
structuralSharing?:
156-
| boolean
157-
| ((oldData: TData | undefined, newData: TData) => TData)
155+
structuralSharing?: boolean | (<T>(oldData: T | undefined, newData: T) => T)
158156
_defaulted?: boolean
159157
/**
160158
* Additional payload to be stored on each query.

packages/react-query/src/__tests__/useQuery.types.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,24 @@ describe('initialData', () => {
155155
})
156156
})
157157
})
158+
159+
describe('structuralSharing', () => {
160+
it('should restrict to same types', () => {
161+
doNotExecute(() => {
162+
const structuralSharing = (
163+
_oldData: number | string | undefined,
164+
newData: number | string,
165+
) => {
166+
return newData
167+
}
168+
169+
useQuery({
170+
queryKey: ['key'],
171+
queryFn: () => 5,
172+
select: String,
173+
structuralSharing,
174+
})
175+
})
176+
})
177+
})
158178
})

0 commit comments

Comments
 (0)