<p dir="auto">Is it possible to use <code class="notranslate">typeof</code> type query operator to get function's return type?</p>
<p dir="auto">It would be helpful if the <code class="notranslate">typeof</code> keyword were changed to support more complex expressions, such as function calls.</p>
<h4 dir="auto">Example</h4>
<p dir="auto">Given the following AngularJS service definition:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="module myApp {
  function myApiFactory($http: HttpSvc) {
    return {
      getSetting: (key: string) =&gt; $http.get(etc),
    };
  }
  angular.service(&quot;myApi&quot;, myApiFactory);
}"><pre class="notranslate"><span class="pl-smi">module</span> <span class="pl-s1">myApp</span> <span class="pl-kos">{</span>
  <span class="pl-k">function</span> <span class="pl-en">myApiFactory</span><span class="pl-kos">(</span><span class="pl-s1">$http</span>: <span class="pl-v">HttpSvc</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-kos">{</span>
      <span class="pl-en">getSetting</span>: <span class="pl-kos">(</span><span class="pl-s1">key</span>: <span class="pl-s1">string</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">$http</span><span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-s1">etc</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
  <span class="pl-s1">angular</span><span class="pl-kos">.</span><span class="pl-en">service</span><span class="pl-kos">(</span><span class="pl-s">"myApi"</span><span class="pl-kos">,</span> <span class="pl-s1">myApiFactory</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">It would be good if the following statement were possible:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="export type MyApi = typeof myApiFactory();"><pre class="notranslate"><span class="pl-s1">export</span> <span class="pl-s1">type</span> <span class="pl-v">MyApi</span> <span class="pl-c1">=</span> <span class="pl-k">typeof</span> <span class="pl-en">myApiFactory</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<h4 dir="auto">Explanation</h4>
<p dir="auto">Since the function is a factory rather than the actual service, this type alias would other components to have a strongly-typed reference to the service. Though this example relates to AngularJS' service factory and dependency injection, this particular change would not be solely for AngularJS support.</p>
<h4 dir="auto">Existing solutions</h4>
<p dir="auto">One option to resolve this issue is to explicitly define an interface for the service. However, this solution would require repeating the signature for all of the service's methods.</p>
<p dir="auto">A workaround exists that relies on JavaScript's scoping rules to satisfy TypeScript's <code class="notranslate">typeof</code> keyword. However, it significantly reduces readability and clarity. An example:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="if (false)
  var obj = myApiFactory(null);
export type MyApi = typeof obj;"><pre class="notranslate"><span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">false</span><span class="pl-kos">)</span>
  <span class="pl-k">var</span> <span class="pl-s1">obj</span> <span class="pl-c1">=</span> <span class="pl-en">myApiFactory</span><span class="pl-kos">(</span><span class="pl-c1">null</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">export</span> <span class="pl-s1">type</span> <span class="pl-v">MyApi</span> <span class="pl-c1">=</span> <span class="pl-k">typeof</span> <span class="pl-s1">obj</span><span class="pl-kos">;</span></pre></div>