HydrationBoundary takes unknown as the type as `unknown` instead of `DehydratedState` #9323
Send to my agentDescribe the bug#
For a library with so much type safety, this is a bit surprising to see.
A refactor to a utility function code using TanStack React Query accidentally passed { state: DehydratedState, ... } instead of the actual dehydrated state, which silently broke some instances of data prefetching on a production Next.js application.
Your minimal, reproducible example#
N/A
Steps to reproduce#
import { HydrationBoundary } from "@tanstack/react-query";
const mistaken = { oh_no: true };
const element = <HydrationBoundary state={mistaken}>hello</HydrationBoundary>;
result: no error!
the real world situation that ran into this was we had a helper function serverSidePrefetchQueries which helps us avoid some of Next.js pitfalls regarding server components. the usage looked like this:
const state = await serverSidePrefetchQueries([
queryThingA,
queryThingB,
});
return <HydrationBoundary state={state}>{client}</HydrationBoundary>
but then the return signature of our helper changed, and these pre-fetches silently stopped working.
Expected behavior#
should not pass type checking
How often does this bug happen?#
None
Screenshots or Videos#
No response
Platform#
- OS: macOS 15
- Browser: N/A
Tanstack Query adapter#
react-query
TanStack Query version#
v4, but issue on v5 as well
TypeScript version#
N/A
Additional context#
No response
TkDodo added labelstypes
I don’t remember any good reason no. I think we might not have had
DehydratedStatein the beginning? (On my phone on shaky vacation internet so can’t check)I think the internal code should try to be defensive and treat this as unknown though, but public type seems like it should for sure be
DehydratedState.@paperclover wanna make a PR to fix this?
@TkDodo opened. I realized while writing that a second issue is that the property is optional, which seems silly because not providing that property makes the component always a no-op. Of course, kept ability to explicitly pass
nullsince that does have a use case.TkDodo closed this
Sign in to join this conversation.
@Ephem do you know why
stateisn’t typed asDehydratedState? We make that assumption in code anyways 🤔