title: RTK-Query conditional typing of BaseQueryFn
body: <p>In RTK, I'm using a custom <code>baseQuery</code> to wrap axios, following the general guidance <a href="https://redux-toolkit.js.org/rtk-query/usage/customizing-queries" rel="nofollow noreferrer">here</a>.  However, I can't seem to figure out a way to use conditional typing for the <code>BaseQueryArg</code> type.  For example, I'd like to be able to have my baseQuery function take in the following argument object:</p>
  <pre><code>{
    url: string
    method: &quot;GET&quot; | &quot;PUT&quot; | &quot;POST&quot; | &quot;DELETE&quot;
    body: any
  }
  </code></pre>
  <p>But, rather than have <code>body</code> be <code>any</code>, or even a stricter union, I'd like to have it vary depend on the type of <code>method</code>.  So, for example, if this was a simple function I would type it like so:</p>
  <pre><code>function myBaseQueryFunction&lt;T extends &quot;GET&quot;|&quot;POST&quot;&gt;(args: {
    url: string
    method: T
    body: T extends &quot;POST&quot; ? {someBodyData: string} : null
  }){
    // do the query
  }
  </code></pre>
  <p>However, I can't seem to find any way to use that kind of typing for the function I pass to <code>baseQuery</code> and have it properly restrict the return types of of my <code>query</code> endpoints.  Even if I don't use the <code>BaseQueryFn</code> utility type and, instead, manually type a function like the one above and then pass it to <code>baseQuery</code>, I still don't get the restriction on <code>query</code>'s return that I want.</p>
  <p>Is there anyway to do this kind of conditional typing of <code>baseQuery</code>?</p>
  <h2>Update</h2>
  <p>Per the accepted answer below, the simplest way to handle this is with a discriminated union on method, e.g.:</p>
  <pre><code>type BaseQueryDiscriminatedArg = 
    | {
        url: string
        method: &quot;POST&quot;
        body: {someBodyData: string}
       }
    | {
        url: string
        method: &quot;GET&quot;
        body?: null
       }
  </code></pre>
  <p>and then your <code>BaseQueryFn</code> type is just <code>BaseQueryFn&lt;BaseQueryDiscriminatedArg&gt;</code></p>