<p dir="auto">Moving over from here: <a href="https://typescript.codeplex.com/workitem/2475" rel="nofollow">https://typescript.codeplex.com/workitem/2475</a></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="type A = number
type B = string
type C = number|string

const cc: C[] = [1, 2, &quot;a&quot;, &quot;b&quot;]

function isNumber(a): a is A {
    return typeof a === 'number'
}

// OK
const dd = []
for (let n = 0; n &lt; cc.length; n++) {
  const d = cc[n]
  if (typeof d === 'number') {
    dd.push(d.toFixed())
  }
}

// OK
const ee = []
for (let n = 0; n &lt; cc.length; n++) {
  const d = cc[n]
  if (isNumber(d)) {
    ee.push(d.toFixed())
  }
}

// NOT OK - property 'toFixed' does not exist on type 'number' | 'string'
// (guard fails when using &quot;cc[n]&quot; instead of first assigning to &quot;d&quot;)
const ff = []
for (let n = 0; n &lt; cc.length; n++) {
  if (typeof cc[n] === 'number') {
    ff.push(cc[n].toFixed())
  }
}

// NOT OK - property 'toFixed' does not exist on type 'number' | 'string'
// (guard fails when guarding the inverse)
const gg = []
for (let n = 0; n &lt; cc.length; n++) {
  const d = cc[n]
  if (typeof d !== 'number') { continue }
  gg.push(d.toFixed())
}

// NOT OK - property 'toFixed' does not exist on type 'number' | 'string'
// (guard fails when used in a filter)
// @see https://github.com/Microsoft/TypeScript/issues/7657
const hh = cc
  .filter(isNumber)
  .map(c =&gt; c.toFixed())"><pre class="notranslate"><span class="pl-k">type</span> <span class="pl-smi">A</span> <span class="pl-c1">=</span> <span class="pl-smi">number</span>
<span class="pl-k">type</span> <span class="pl-smi">B</span> <span class="pl-c1">=</span> <span class="pl-smi">string</span>
<span class="pl-k">type</span> <span class="pl-smi">C</span> <span class="pl-c1">=</span> <span class="pl-smi">number</span><span class="pl-c1">|</span><span class="pl-smi">string</span>

<span class="pl-k">const</span> <span class="pl-s1">cc</span>: <span class="pl-smi">C</span><span class="pl-kos">[</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">,</span> <span class="pl-c1">2</span><span class="pl-kos">,</span> <span class="pl-s">"a"</span><span class="pl-kos">,</span> <span class="pl-s">"b"</span><span class="pl-kos">]</span>

<span class="pl-k">function</span> <span class="pl-en">isNumber</span><span class="pl-kos">(</span><span class="pl-s1">a</span><span class="pl-kos">)</span>: <span class="pl-s1">a</span> is <span class="pl-smi">A</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-k">typeof</span> <span class="pl-s1">a</span> <span class="pl-c1">===</span> <span class="pl-s">'number'</span>
<span class="pl-kos">}</span>

<span class="pl-c">// OK</span>
<span class="pl-k">const</span> <span class="pl-s1">dd</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span>
<span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">let</span> <span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-s1">n</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">cc</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span> <span class="pl-s1">n</span><span class="pl-c1">++</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">const</span> <span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">cc</span><span class="pl-kos">[</span><span class="pl-s1">n</span><span class="pl-kos">]</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-s1">d</span> <span class="pl-c1">===</span> <span class="pl-s">'number'</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">dd</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">d</span><span class="pl-kos">.</span><span class="pl-en">toFixed</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-c">// OK</span>
<span class="pl-k">const</span> <span class="pl-s1">ee</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span>
<span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">let</span> <span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-s1">n</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">cc</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span> <span class="pl-s1">n</span><span class="pl-c1">++</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">const</span> <span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">cc</span><span class="pl-kos">[</span><span class="pl-s1">n</span><span class="pl-kos">]</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-en">isNumber</span><span class="pl-kos">(</span><span class="pl-s1">d</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">ee</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">d</span><span class="pl-kos">.</span><span class="pl-en">toFixed</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-c">// NOT OK - property 'toFixed' does not exist on type 'number' | 'string'</span>
<span class="pl-c">// (guard fails when using "cc[n]" instead of first assigning to "d")</span>
<span class="pl-k">const</span> <span class="pl-s1">ff</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span>
<span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">let</span> <span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-s1">n</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">cc</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span> <span class="pl-s1">n</span><span class="pl-c1">++</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-s1">cc</span><span class="pl-kos">[</span><span class="pl-s1">n</span><span class="pl-kos">]</span> <span class="pl-c1">===</span> <span class="pl-s">'number'</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">ff</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">cc</span><span class="pl-kos">[</span><span class="pl-s1">n</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-en">toFixed</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-c">// NOT OK - property 'toFixed' does not exist on type 'number' | 'string'</span>
<span class="pl-c">// (guard fails when guarding the inverse)</span>
<span class="pl-k">const</span> <span class="pl-s1">gg</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span>
<span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">let</span> <span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-s1">n</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">cc</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span> <span class="pl-s1">n</span><span class="pl-c1">++</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">const</span> <span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">cc</span><span class="pl-kos">[</span><span class="pl-s1">n</span><span class="pl-kos">]</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-s1">d</span> <span class="pl-c1">!==</span> <span class="pl-s">'number'</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">continue</span> <span class="pl-kos">}</span>
  <span class="pl-s1">gg</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">d</span><span class="pl-kos">.</span><span class="pl-en">toFixed</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span>

<span class="pl-c">// NOT OK - property 'toFixed' does not exist on type 'number' | 'string'</span>
<span class="pl-c">// (guard fails when used in a filter)</span>
<span class="pl-c">// <span class="pl-k">@see</span> https://github.com/Microsoft/TypeScript/issues/7657</span>
<span class="pl-k">const</span> <span class="pl-s1">hh</span> <span class="pl-c1">=</span> <span class="pl-s1">cc</span>
  <span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">isNumber</span><span class="pl-kos">)</span>
  <span class="pl-kos">.</span><span class="pl-en">map</span><span class="pl-kos">(</span><span class="pl-s1">c</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">c</span><span class="pl-kos">.</span><span class="pl-en">toFixed</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span></pre></div>