Skip to content

Commit

Permalink
fix(react-query): fix inferring data as | undefined when using initia…
Browse files Browse the repository at this point in the history
…lData without queryFn (#8674)
  • Loading branch information
gs18004 authored Feb 20, 2025
1 parent d07a3db commit 9ac54b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ describe('initialData', () => {
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
}
})

it('data should not have undefined when initialData is provided', () => {
const { data } = useQuery({
queryKey: ['query-key'],
initialData: 42,
})

expectTypeOf(data).toEqualTypeOf<number>()
})
})

describe('custom hook', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-query/src/queryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type DefinedInitialDataOptions<
initialData:
| NonUndefinedGuard<TQueryFnData>
| (() => NonUndefinedGuard<TQueryFnData>)
queryFn: QueryFunction<TQueryFnData, TQueryKey>
queryFn?: QueryFunction<TQueryFnData, TQueryKey>
}

export function queryOptions<
Expand Down

0 comments on commit 9ac54b1

Please sign in to comment.