title: Given the type "anything except function OR function", how do I check if it's a function in TypeScript?
body: <p>Why is the <code>value</code> not of type <code>() =&gt; string</code> after the <code>if (typeof value === 'function')</code> check?</p>
  
  <p>What check should be made, so that the type is inferred correctly (making the cast unnecessary)?</p>
  
  <pre><code>function foo&lt;T extends {}&gt;(value: Exclude&lt;T, Function&gt; | (() =&gt; string)) {
      let result: string;
  
      if (typeof value === 'function') {
          result = value(); // Error: Type '(() =&gt; string) | (Exclude&lt;T, Function&gt; &amp; Function)' has no compatible call signatures.
          result = (value as () =&gt; string)(); // works, but I don't like it
      } else {
          result = value.toString();
      }
  
      console.log(result);
  }
  </code></pre>