3

The definition for a query in RTK Query is query<ResultType, QueryArg>. All of this is fine for queries where I actually need to send parameters to the API. However, I have some endpoints which don't require anything to be sent (something like getAllX). What can I pass as QueryArg here in the template? It won't accept having one argument.

So far I've used types like undefined and null to denote that nothing needs to be sent, but it looks ugly when you use the hook: const {...} = useGetXQuery(undefined), and I'm pretty sure there has to be a better way, but scouring the internet yielded no results.

k_krrs
  • 139
  • 7
  • Have you tried passing empty object like `{}`? It's way cleaner – JkAlombro May 10 '23 at 07:54
  • @JkAlombro yup, I was just hoping to have no parameters in there. I found the solution though, posted it as an answer. I totally forgot about the `void` type. – k_krrs May 10 '23 at 07:57

1 Answers1

4

Figured it out, you need to use void as a type, then you can use the hook like this: const {...} = useGetXQuery().

k_krrs
  • 139
  • 7
  • 1
    FWIW this _is_ mentioned in our docs, at https://redux-toolkit.js.org/rtk-query/usage-with-typescript#typing-query-and-mutation-endpoints , although it's sorta buried: "If `query` doesn't have a parameter, then `void` type has to be provided explicitly." – markerikson May 11 '23 at 23:42
  • Ah, thanks @markerikson, I completely missed it. – k_krrs May 12 '23 at 11:57
  • I missed this too. I think this should be more conspicuous some examples online use undefined but it's ugly this one is more correct. cc: @markerikson – JohnnyQ Jul 17 '23 at 00:37