Skip to content
Source
Sign inSign up
TanStack/query

Statically enforce query key is JSON serializable #8368

Send to my agent
ClosedOliverJAsh(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

Currently the types do not prevent passing a query key that is not JSON serializable.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-pyljd3?file=src%2Fmain.tsx&terminal=dev

Steps to reproduce

import { QueryClient } from '@tanstack/react-query';

const queryClient = new QueryClient({});

const obj = () => {};
queryClient.fetchQuery({
  // Expected error. Query key is not JSON serializable.
  queryKey: ['foo', obj],
  queryFn: () => Promise.resolve('bar'),
});

Expected behavior

Ideally we would get a type error when trying to pass a query key that is not JSON serializable.

I believe this could be enforced by changing the default QueryKey type from ReadonlyArray<unknown> to ReadonlyArray<Json>:

+type Json = boolean | number | string | null | JsonArray | JsonRecord
+interface JsonRecord { readonly [key: string]: Json }
+interface JsonArray extends ReadonlyArray<Json> {}
-type QueryKey = ReadonlyArray<unknown>;
+type QueryKey = ReadonlyArray<Json>;

How often does this bug happen?

Often

Screenshots or Videos

No response

Platform

N/A

Tanstack Query adapter

None

TanStack Query version

5.61.5

TypeScript version

5.7

Additional context

No response

  1. This isn’t easily doable because you can also provide a custom queryKeyHashFn that would then take care of serializing things that aren’t json serializable per se.

    If you think we can work on the docs for this, please file a PR. Otherwise, this isn’t something we’re going to focus on right now.

  2. TkDodo closed this

  3. TkDodo added labelswontfix

  4. @TkDodo Thanks for the reply!

    Couldn't we do something like this?

    type Json = boolean | number | string | null | JsonArray | JsonRecord;
    interface JsonRecord {
      readonly [key: string]: Json;
    }
    interface JsonArray extends ReadonlyArray<Json> {}
    
    type Options =
      | { queryKey: ReadonlyArray<Json> }
      | {
          queryKey: ReadonlyArray<unknown>;
          queryKeyHashFn: (key: ReadonlyArray<unknown>) => string;
        };
    
    declare const fetchQuery: (x: Options) => void;
    
    const obj = () => {};
    
    fetchQuery({
      // Error as expected ✅. Query key is not JSON serializable.
      queryKey: ['foo', obj],
    });
    
    fetchQuery({
      // No error ✅
      queryKey: ['foo', 'bar'],
    });
    
    fetchQuery({
      // No error ✅
      queryKey: ['foo', obj],
      queryKeyHashFn: (key) => key.join(','),
    });
    
  5. The queryKeyHashFn can also be set globally, and likely, that's also what users will do to get support to serialize custom structures for all queries.

  6. Ahh okay, I was thinking those users could still use a type assertion as an escape hatch. I suppose it's a trade-off between type safety and DX for users who set a global queryKeyHashFn.

Sign in to join this conversation.