Fitim BytyqiF
tRPC9mo ago
1 reply
Fitim Bytyqi

TRPC 11 Classic React Query custom hook

This is how I was able to refactor one of the custom hooks I had before, typescript threw an error ofc.

old approach
export const useGlassDiameters = <
  T extends DecoratedQuery<{
    input: DiameterRequestDto;
    output: DiameterResponseDto[];
    transformer: any;
    errorShape: any;
  }>,
  U extends T extends DecoratedQuery<infer R> ? R : never,
>(
  input: U['input'] | SkipToken,
  opts?: UseTRPCQueryOptions<U['output'], any, any>
): UseTRPCQueryResult<U['output'], TRPCClientErrorLike<U>> =>
  trpc.glassesPrescription.getGlassDiameters.useQuery(input, opts);



Refactored approach
  export const useGlassDiameters = (
    input: DiameterRequestDto,
    opts?: UseTRPCQueryOptions<DiameterResponseDto[], any, any>
  ): UseTRPCQueryResult<DiameterResponseDto[], TRPCClientErrorLike<any>> =>
    trpc.glassesPrescription.getGlassDiameters.useQuery(input, opts);


I would like to know your thoughts if this is the right way.
Was this page helpful?