vishalbalaji / trpc-svelte-query-adapter

A simple adapter to use `@tanstack/svelte-query` with trpc, similar to `@trpc/react-query`.
71 stars 6 forks source link

Type error when using getQueryKey #35

Closed smaven closed 4 months ago

smaven commented 4 months ago

I have the following procedure:

export const userRouter = {
  all: publicProcedure
    .input(
      z
        .object({
          name: z.string(),
        })
        .optional(),
    )
    .query(async ({ input }) => {
      return 'something'
    }),
} satisfies TRPCRouterRecord;

Trying to get the query key gives me the following type error:

image

Seems like the type GetQueryKey is not accounting for the void type

type GetQueryKey<TInput = undefined> = TInput extends undefined
    ? {
        [ProcedureNames.queryKey]: () => QueryKey
    }
    : {
        /**
         * Method to extract the query key for a procedure
         * @param type - defaults to `any`
         */
        [ProcedureNames.queryKey]: (input: TInput, type?: QueryType) => QueryKey
    } & {}

This can be fixed by modifying the type as:

type GetQueryKey<TInput = undefined> = [TInput] extends [undefined] | [void]
    ? {
        [ProcedureNames.queryKey]: () => QueryKey
    }
    : {
        /**
         * Method to extract the query key for a procedure
         * @param type - defaults to `any`
         */
        [ProcedureNames.queryKey]: (input: TInput, type?: QueryType) => QueryKey
    } & {}

If it looks good, I'll be happy to create a PR for this fix.

P.S. Great work on this package! 🙌

vishalbalaji commented 4 months ago

Hey @smaven thanks for letting me know! I suppose [TInput] extends [undefined] | [void] could be re-written as [TInput] extends [undefined | void]. Other than that, I would be happy to merge this PR if you were to raise one.