Skip to content
Source
Sign inSign up
TanStack/query

setQueryData<T | undefined>(key, objectLiteral) does not compile since 5.100.13 (built-in NoInfer) #12695

Closedbeshkenadze(GitHub profile, opens in a new tab) opened this issue 1 comment

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

https://www.typescriptlang.org/play/?ts=5.9.3#code/PTAEEcFcFMCcE8C0BjA9raoB8BeUBWAOgEYAGUkgZlABd4AHaAZ1oAtMnoaBFGBAEQCGNQaEj0AJsLigA7gEsarNpgBGkeQBsaieQDtQAOVQBJPQDMZACgDEZfAE5KASkIAoOo1ABVSdNgAPAAqZvSQNAA0oEEA8uFhNFigeLHx4aAAPqBWVvoJAFzRoeHOyUmpNAnObhLQyJqCGKDmkHrINPKoBpw8fPBCIsFYVm6gYn40cIW+UpOBxmaWgUFJWa215vrQElELFnBDmWJ6G1sSWBFuzoUAbqjyEm5uIBB9KOiYAXhEZBTEAExiTgSNjyFiCTTyQRMdyeTAxTQSPZLQ54ADaQQAuhjQNAAB6TE7gvTwUAAflApFAhT00BucExNTqDSaLTaHS6oFQiIAylxeHB+sJBEMRmNxLMpj4JgcEUjTPtlqtjqdaTtQHLkQcVkd1tBNmqLldbvdHh4GJgBqI8ABvUCCGmQAC2qhkAF8nj0BQJhQErbqTvqzsM7Q7QMRQG7Si84LB0NEef9KAAWfBubkSPm9QVWv3CgOq7Yh+2FCNR0AvVAAazcXr6uatxbDZejYGrbiAA

Steps to reproduce

  1. Open the Playground link (TypeScript 5.9.3).
  2. Line 15 shows TS2345. Lines 16 and 17 compile.

With the package:

  1. Install @tanstack/query-core@5.108.4 and typescript@6.0.3. Use "strict": true.
  2. Compile the snippet above. The first setQueryData call shows TS2345.
  3. 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.

  1. tanstack-source added labelsbug

  2. setQueryData<T | undefined> doesn’t seem legit, we add undefined to the union for you. So this both works:

    queryClient.setQueryData<Data>(['a'], { status: 200, value: 'a' })
    queryClient.setQueryData<Data>(['a'], undefined)
  3. TkDodo closed this

  4. TkDodo added labelsworks as designed

Sign in to join this conversation.