title: Infer arguments of an object's method
body: <p>I'm trying to infer the arguments of a method on an arbitrary object passed into a function:</p>
  <p>e.g.</p>
  <pre><code>const param = getInferredParam(obj, 'api');
  </code></pre>
  <p>where obj is an object of type:</p>
  <pre><code>{
    api: (...params: any[]) =&gt; Promise&lt;any&gt;
  }
  </code></pre>
  <p>I'm trying to infer the <code>params</code> type like this:</p>
  <pre><code>type ObjType = { api: ((...params: any[]) =&gt; Promise&lt;any&gt;) };
  
  type Params&lt;T&gt; = T extends { api: ((...params: infer R) =&gt; Promise&lt;any&gt;) } ? R : any[];
  
  const obj = {
    api: (id: string) =&gt; new Promise((resolve) =&gt; {
      resolve(id);
    })
  }
  
  function getInferredParam&lt;T extends ObjType&gt;(anyObj: T, key: keyof T) {
    const prop = anyObj[key];
  
    return function params(...p: Params&lt;typeof prop&gt;) {
        return p;
      }
  }
  
  const param = getInferredParam(obj, 'api');
  </code></pre>
  <p>But the inference of <code>param</code> is always <code>const param: (...p: any[]) =&gt; any[]</code>.</p>
  <p>How can I infer these params correctly?</p>
  <p><a href="https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKwCrmgXigbygQzASwC4oAKEgOkrBwCccBbAZ2JwDsQBtAXQEoo0AfFAAKNAPb18jCAB42IAXwC%20AbgCwAKE2hII2g0YykQjEigQAHsAisAJoyy4CxMpXLU6TYvlYAzCDRQAEp8giLiktJy7IpQSlAA-MFQLOzc6loaAMZirIzAUGKI-FiaUE5EpPi2xPk0PgDmoUKsEADu4RJSEGQ0EIxiADYAbhDNpRrl5X0DIz3VPBnlSjyaSpqavgCurFnA%20LlQDRDAAJJ%20AX22wvr0RuZWNvawiCiQAiTy8AjESAA0UAA1hAQMRgSAxL4oEg%20JgylAcnkCmBxGASl9EBxwVwMvC%20sAtjRWFBtrt9ocPAYKFRiDdPIYdBBIVAUWIwLE4ZMptMToTiWAllN1hphZpEfkWbcSsczhcaFc6QwSEUEACAOR4fBqxZAA" rel="nofollow noreferrer">Playground</a></p>