Skip to content

Commit

Permalink
fix(queryClient): make sure that setQueryData can return undefined fr…
Browse files Browse the repository at this point in the history
…om the updater function on type level (#3615)

the only runtime tests we had didn't use the previous value, so the generic defaults to unknown; the TS error becomes apparent when providing a generic to setQueryData
  • Loading branch information
TkDodo committed May 14, 2022
1 parent 0085137 commit 35a40ec
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class QueryClient {

setQueryData<TData>(
queryKey: QueryKey,
updater: Updater<TData | undefined, TData> | undefined,
updater: Updater<TData | undefined, TData | undefined>,
options?: SetDataOptions
): TData | undefined {
const query = this.queryCache.find<TData>(queryKey)
Expand Down
4 changes: 2 additions & 2 deletions src/core/tests/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ describe('queryClient', () => {

test('should not update query data if updater returns undefined', () => {
const key = queryKey()
queryClient.setQueryData(key, 'qux')
queryClient.setQueryData(key, () => undefined)
queryClient.setQueryData<string>(key, 'qux')
queryClient.setQueryData<string>(key, () => undefined)
expect(queryClient.getQueryData(key)).toBe('qux')
})

Expand Down

0 comments on commit 35a40ec

Please sign in to comment.