title: How to declare return type based on parameter's type in Typescript
body: <p>I want to infer a return type based on the parameter's type.</p>
  <p>Here is my try</p>
  <pre><code>type Arg = string | (() =&gt; string)
  
  function fn1(arg: Arg): typeof arg extends Function ? () =&gt; string : string {
    if (typeof arg === &quot;function&quot;) {
      return () =&gt; arg();
    }
  
    return arg;
  }
  
  const a = fn1(&quot;hello&quot;) // a should be &quot;string&quot;
  const b = fn1(() =&gt; &quot;hello&quot;) // b should be () =&gt; &quot;string&quot;
  
  </code></pre>
  <p><a href="https://www.typescriptlang.org/play?#code/C4TwDgpgBAggTgcygXigZ2HAlgOyQHygAoiBKFAPnU1wVIFgAoJgMwFccBjYLAexygscARiIBDRAC5YiUtNCReLKBKQQAHsAg4AJmmLlkVDNjxQA-AcrVTSaSdpQA3kyhQsyogohKViFMioAETsXDz8QeQujG5ucBDAbHACZNaqZADcrlAAvkzZ8YnJfghZjHnMjJz8GCoogiJEQQAWEAA2bbyRUAD0PXVozbxsbTpQAEbQQQ54QUzVOLXj9UKiqUZQLe2d3X0T6EMjY5NWG9M0s1BMQA" rel="nofollow noreferrer">Link to demo</a></p>
  <p>Unfortunately I have no idea why typescript fails on line <code>return () =&gt; arg()</code> with an error <code>Type '() =&gt; string' is not assignable to type 'string'</code> where this line is in a if statement.</p>