TanStackT
TanStack3mo ago
4 replies
rubber-blue

obscure getKey type error on collection options

If I have a property in my schema that I have chosen as id for queryCollectionOptions and it is optional
then I see a long type error on the entire config object (inside queryCollectionOptions)

const schemaExample = z.object({
    id: z.string().uuid().optional(),
    // ...
});
type SchemaExample = z.infer<typeof schemaExample>;

// Define collection with persistence handlers
const exampleCollection = createCollection(
    queryCollectionOptions<SchemaExample>({
        queryKey: ['examples'],
        queryFn: ({ queryKey }) => [],
        queryClient,
        getKey: (item) => item.id,
    }),
);


No overload matches this call.
  The last overload gave the following error.
    Argument of type 'CollectionConfig<Record<string, unknown>, string | number, { id?: string | undefined; }, UtilsRecord> & { schema: { id?: string | undefined; }; utils: QueryCollectionUtils<...>; }' is not assignable to parameter of type 'CollectionConfig<Record<string, unknown>, string | number, never, UtilsRecord> & { schema?: undefined; utils?: (UtilsRecord & QueryCollectionUtils<...>) | undefined; } & SingleResult'.
      Type 'CollectionConfig<Record<string, unknown>, string | number, { id?: string | undefined; }, UtilsRecord> & { schema: { id?: string | undefined; }; utils: QueryCollectionUtils<...>; }' is not assignable to type 'CollectionConfig<Record<string, unknown>, string | number, never, UtilsRecord>'.
        Types of property 'schema' are incompatible.
          Type '{ id?: string | undefined; }' is not assignable to type 'undefined'.ts(2769)
index.d.ts(106, 25): The last overload is declared here.
⚠ Error (TS2769)  | No overload matches this call.


I understand the error, but why isn't the error related only to the getKey property instead of the entire config?)
Was this page helpful?