<p dir="auto">The TypeScript definition for Array.filter specifies the callback must return a boolean, thus the code example at the bottom of this post fails to compile.  Here's the current definition:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="filter(callbackfn: (value: T, index: number, array: T[]) =&gt; boolean, thisArg?: any): T[];"><pre class="notranslate"><code class="notranslate">filter(callbackfn: (value: T, index: number, array: T[]) =&gt; boolean, thisArg?: any): T[];
</code></pre></div>
<p dir="auto"><a href="https://tc39.github.io/ecma262/#sec-array.prototype.filter" rel="nofollow">The spec</a> says this about the callback parameter:</p>
<blockquote>
<p dir="auto">callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false.</p>
</blockquote>
<p dir="auto">Thus I propose the definition should be changed to:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="filter(callbackfn: (value: T, index: number, array: T[]) =&gt; any, thisArg?: any): T[];"><pre class="notranslate"><code class="notranslate">filter(callbackfn: (value: T, index: number, array: T[]) =&gt; any, thisArg?: any): T[];
</code></pre></div>
<p dir="auto"><em>Note: using a double not-operator is a sufficient workaround with negligible implications</em></p>
<p dir="auto"><strong>TypeScript Version:</strong><br>
1.8.7</p>
<p dir="auto"><strong>Code</strong><br>
<a href="https://www.typescriptlang.org/play/#src=let%20a%20%3D%20%5B1%2C%202%2C%203%2C%20undefined%2C%200%2C%2042%2C%20%22%22%5D.filter%28i%20%3D%3E%20i%29%0D%0A%0D%0Aconsole.info%28a%29" rel="nofollow">Playground</a></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// the purpose is to weed out falsey values
let a = [1, 2, 3, undefined, 0, 42, &quot;&quot;].filter(i =&gt; i)
// &quot;i =&gt; i&quot; is an error because it does not return boolean
// the following has no error
let b = [1, 2, 3, undefined, 0, 42, &quot;&quot;].filter(i =&gt; !!i)"><pre class="notranslate"><span class="pl-c">// the purpose is to weed out falsey values</span>
<span class="pl-k">let</span> <span class="pl-s1">a</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-c1">3</span><span class="pl-kos">,</span> <span class="pl-c1">undefined</span><span class="pl-kos">,</span> <span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-c1">42</span><span class="pl-kos">,</span> <span class="pl-s">""</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">i</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">i</span><span class="pl-kos">)</span>
<span class="pl-c">// "i =&gt; i" is an error because it does not return boolean</span>
<span class="pl-c">// the following has no error</span>
<span class="pl-k">let</span> <span class="pl-s1">b</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-c1">3</span><span class="pl-kos">,</span> <span class="pl-c1">undefined</span><span class="pl-kos">,</span> <span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-c1">42</span><span class="pl-kos">,</span> <span class="pl-s">""</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">i</span> <span class="pl-c1">=&gt;</span> <span class="pl-c1">!</span><span class="pl-c1">!</span><span class="pl-s1">i</span><span class="pl-kos">)</span></pre></div>
<p dir="auto"><strong>Expected behavior:</strong><br>
No compile errors</p>
<p dir="auto"><strong>Actual behavior:</strong><br>
Compile errors</p>
<p dir="auto">From here: <a href="https://typescript.codeplex.com/workitem/2464" rel="nofollow">https://typescript.codeplex.com/workitem/2464</a></p>
<p dir="auto">I have a possible solution: Just allow this:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class A {
    [index: number]: A; // &lt;- since you can already do this
    (...args): A; // &lt;- why not allow this also?
    constructor(...args) { }
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">A</span> <span class="pl-kos">{</span>
    <span class="pl-kos">[</span><span class="pl-s1">index</span>: <span class="pl-smi">number</span><span class="pl-kos">]</span>: <span class="pl-smi">A</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;- since you can already do this</span><span class="pl-c1"></span>
    <span class="pl-kos">(</span>...<span class="pl-s1">args</span><span class="pl-kos">)</span>: <span class="pl-smi">A</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;- why not allow this also?</span>
    <span class="pl-en">constructor</span><span class="pl-kos">(</span>...<span class="pl-s1">args</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Other suggestion by fdecampredon was:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class A {
  (): string {
    //do something
    return 'hello world';
  }
  constructor(): A {
    //do something else
  }
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">A</span> <span class="pl-kos">{</span><span class="pl-en"></span>
  <span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">string</span> <span class="pl-kos">{</span>
    <span class="pl-c">//do something</span>
    <span class="pl-k">return</span> <span class="pl-s">'hello world'</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
  <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">A</span> <span class="pl-kos">{</span>
    <span class="pl-c">//do something else</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">could be transformed into that :</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function A() {
  if (!(this instanceof A)) {
    //do something
    return 'hello world';
  } else {
    //do something else
  }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-smi">A</span><span class="pl-kos">(</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-c1">!</span><span class="pl-kos">(</span><span class="pl-smi">this</span> <span class="pl-k">instanceof</span> <span class="pl-smi">A</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">//do something</span>
    <span class="pl-k">return</span> <span class="pl-s">'hello world'</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
    <span class="pl-c">//do something else</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>