<p dir="auto"><em>From <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jwulf/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jwulf">@jwulf</a> on November 24, 2015 12:49</em></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/406975/11367128/6a53fb2e-92fd-11e5-911c-752e64b96a2f.png"><img src="https://cloud.githubusercontent.com/assets/406975/11367128/6a53fb2e-92fd-11e5-911c-752e64b96a2f.png" alt="screen shot 2015-11-24 at 10 43 58 pm" style="max-width: 100%;"></a></p>
<p dir="auto"><em>Copied from original issue: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="118602073" data-permission-text="Title is private" data-url="https://github.com/microsoft/vscode/issues/544" data-hovercard-type="issue" data-hovercard-url="/microsoft/vscode/issues/544/hovercard" href="https://github.com/microsoft/vscode/issues/544">microsoft/vscode#544</a></em></p>
<p dir="auto"><strong>TypeScript Version:</strong></p>
<p dir="auto">nightly (1.9.0-dev.20160217)</p>
<p dir="auto"><strong>Code</strong><br>
A contrived example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="interface Convert {
  (arg: string): number;
  (arg: number): string;
}

function convert(arg: number | string): number | string {
  return typeof arg === 'number' ? arg.toString() : parseFloat(arg);
}

let foo: Convert = convert;"><pre class="notranslate"><span class="pl-k">interface</span> <span class="pl-smi">Convert</span> <span class="pl-kos">{</span>
  <span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
  <span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">number</span><span class="pl-kos">)</span>: <span class="pl-smi">string</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-en">convert</span><span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">number</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span><span class="pl-kos">)</span>: <span class="pl-smi">number</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-k">typeof</span> <span class="pl-s1">arg</span> <span class="pl-c1">===</span> <span class="pl-s">'number'</span> ? <span class="pl-s1">arg</span><span class="pl-kos">.</span><span class="pl-en">toString</span><span class="pl-kos">(</span><span class="pl-kos">)</span> : <span class="pl-en">parseFloat</span><span class="pl-kos">(</span><span class="pl-s1">arg</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">let</span> <span class="pl-s1">foo</span>: <span class="pl-smi">Convert</span> <span class="pl-c1">=</span> <span class="pl-s1">convert</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Expected behavior:</strong><br>
Code above compiles, and the following compiles and runs without errors:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let result1: string = foo(10);
let result2: number = foo(result1);
assert(result2 === 10);"><pre class="notranslate"><span class="pl-k">let</span> <span class="pl-s1">result1</span>: <span class="pl-smi">string</span> <span class="pl-c1">=</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-c1">10</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">let</span> <span class="pl-s1">result2</span>: <span class="pl-smi">number</span> <span class="pl-c1">=</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">result1</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-en">assert</span><span class="pl-kos">(</span><span class="pl-s1">result2</span> <span class="pl-c1">===</span> <span class="pl-c1">10</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Actual behavior:</strong><br>
Compilation error:</p>
<blockquote>
<p dir="auto">...(10,5): error TS2322: Type '(arg: number | string) =&gt; number | string' is not assignable to type 'Convert'.<br>
Type 'number | string' is not assignable to type 'number'.<br>
Type 'string' is not assignable to type 'number'.</p>
</blockquote>
<p dir="auto"><strong>Current approach:</strong><br>
Specify all overloads again above function:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="interface Convert {
  (arg: string): number;
  (arg: number): string;
}

function convert(arg: string): number;
function convert(arg: number): string;
function convert(arg: number | string): number | string {
  return typeof arg === 'number' ? arg.toString() : parseFloat(arg);
}

let foo: Convert = convert;"><pre class="notranslate"><span class="pl-k">interface</span> <span class="pl-smi">Convert</span> <span class="pl-kos">{</span>
  <span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
  <span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">number</span><span class="pl-kos">)</span>: <span class="pl-smi">string</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-s1">convert</span><span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
<span class="pl-k">function</span> <span class="pl-s1">convert</span><span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">number</span><span class="pl-kos">)</span>: <span class="pl-smi">string</span><span class="pl-kos">;</span>
<span class="pl-k">function</span> <span class="pl-en">convert</span><span class="pl-kos">(</span><span class="pl-s1">arg</span>: <span class="pl-smi">number</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span><span class="pl-kos">)</span>: <span class="pl-smi">number</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-k">typeof</span> <span class="pl-s1">arg</span> <span class="pl-c1">===</span> <span class="pl-s">'number'</span> ? <span class="pl-s1">arg</span><span class="pl-kos">.</span><span class="pl-en">toString</span><span class="pl-kos">(</span><span class="pl-kos">)</span> : <span class="pl-en">parseFloat</span><span class="pl-kos">(</span><span class="pl-s1">arg</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">let</span> <span class="pl-s1">foo</span>: <span class="pl-smi">Convert</span> <span class="pl-c1">=</span> <span class="pl-s1">convert</span><span class="pl-kos">;</span></pre></div>