setQueryData<T | undefined>(key, objectLiteral) does not compile since 5.100.13 (built-in NoInfer) #12695
Describe the bug#
Since @tanstack/query-core 5.100.13, setQueryData refuses an object literal when the type argument includes undefined:
import { QueryClient } from '@tanstack/query-core'
type Data = { status: number; value: string }
const queryClient = new QueryClient()
const data: Data = { status: 200, value: 'a' }
// Fails:
queryClient.setQueryData<Data | undefined>(['a'], { status: 200, value: 'a' })
// Compile:
queryClient.setQueryData<Data | undefined>(['b'], data)
queryClient.setQueryData<Data | undefined>(['c'], () => ({ status: 200, value: 'a' }))
queryClient.setQueryData<Data>(['d'], { status: 200, value: 'a' })
error TS2345: Argument of type '{ status: number; value: string; }' is not assignable to parameter of type 'Updater<NoInfer<Data | undefined> | undefined, NoInfer<Data | undefined> | undefined>'.
Types of property 'status' are incompatible.
Type 'number' is not assignable to type 'undefined'.
5.100.12 compiles all four calls. I checked each query-core release from 5.90.20 to 5.108.4 (except 5.94.4, which tsc cannot resolve). 5.100.13 is the first release that fails, and all later releases fail. 5.100.13 has one change, #10593. It replaces [T][T extends any ? 0 : never] with the built-in NoInfer. The rest of the setQueryData signature is the same in 5.100.12 and 5.100.13.
The same error occurs without TanStack Query (see the Playground link). With the built-in NoInfer, Updater<NoInfer<T> | undefined, NoInfer<T> | undefined> refuses the object literal. With the old alias, it accepts it. Thus the cause can be in TypeScript. microsoft/TypeScript#61076 (NoInfer that wraps a union) can be related. I did not find a TypeScript issue for this case in a short search.
Related, not the same: #9937, #11244, #11795.
Your minimal, reproducible example#
Steps to reproduce#
- Open the Playground link (TypeScript 5.9.3).
- Line 15 shows TS2345. Lines 16 and 17 compile.
With the package:
- Install
@tanstack/query-core@5.108.4andtypescript@6.0.3. Use"strict": true. - Compile the snippet above. The first
setQueryDatacall shows TS2345. - Install
@tanstack/query-core@5.100.12. The snippet compiles.
Expected behavior#
setQueryData<Data | undefined>(key, { ... }) compiles, as in 5.100.12.
A workaround is to give the type without undefined: setQueryData<Data>(key, { ... }). The signature adds | undefined itself.
How often does this bug happen?#
Every time
Platform#
Type check only (tsc). macOS.
TanStack Query adapter#
react-query (the type is in query-core)
TanStack Query version#
5.108.4 (first failing release: 5.100.13)
TypeScript version#
5.9.3, 6.0.3, 7.0.2
Additional context#
In the Playground code, TypeScript 5.4.5 also refuses setQueryData<Data>({ a: 1 }) (TS2353). TypeScript 5.9.3 accepts it.
tanstack-source added labelsbug
TkDodo closed this
TkDodo added labelsworks as designed
Sign in to join this conversation.
setQueryData<T | undefined>doesn’t seem legit, we addundefinedto the union for you. So this both works: