Skip to content

feat: #7266

Description

@barroudjo

Describe the feature you'd like to request

Allow creating custom hooks that have the same types (for both arguments and return) as when using useQuery.
Simple example, assuming I have a TRPC server that provides a route trpcClient.user.searchByPseudo(input) where input is a string and the return is of the type User.

const useSearchUserByPseudoQuery = <
  T extends Parameters<typeof trpc.user.searchByPseudo.queryOptions>[1],
>(
  input: string,
  opts?: T
) => {
  const query = useQuery(trpc.user.searchByPseudo.queryOptions(input, opts));

  useEffect(() => {
    // doing something with query.data for example
  }, [query.data]);

  return query;
};

Unfortunately that new hook won't have the same types inferred for its return as when using the original hook:

const MyComponent = () => {
  const { data: dataFromUseQuery } = useQuery(trpc.user.searchByPseudo.queryOptions('foo');
  // dataFromUseQuery type is as expected: User | undefined

  const { data: dataFromOurHook } = useSearchUserByPseudoQuery('foo');
  // dataFromUseQuery type is incorrect: {}
}

Describe the solution you'd like to see

I've no idea as to how to do it ! I can just say the types should be kept. I've tried many ways, but I haven't been able to find one, so I assume a change is needed in @trpc/tanstack-react-query.

Describe alternate solutions

There are no alternate solutions, the return type should be kept.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions