title: How to make a generic filter that returns the array elements passed in generic <T>
body: <p>I have a <code>Parent</code> interface which is extended by <code>ChildA</code> and <code>ChildB</code>;</p>
  
  <pre><code>interface Parent {
    sharedProp: string,
  }
  
  interface ChildA extends Parent { 
    onlyInA: string,
  }
  
  interface ChildB extends Parent { 
    onlyInB: number
  }
  
  </code></pre>
  
  <p>I have an array that contains the union array types:</p>
  
  <pre><code>const entities: (ChildA|ChildB)[] = [
    {
      sharedProp: "this will be B",
      onlyInA: "because Im a"
    }, {
      sharedProp: "this will be B",
      onlyInB: 12345
    }
  ]
  </code></pre>
  
  <p>I've made the non generic function that does what I want:</p>
  
  <pre><code>const childrenWithCertainSharedProp = (sharedProp: string): ChildA[] =&gt; {
      return entities
          .filter(isChildA)
          .filter(e =&gt; e.sharedProp === sharedProp);
  }
  
  </code></pre>
  
  <p>I could do <code>console.log(childrenWithCertainSharedProp('xxx'));</code></p>
  
  <p><a href="https://www.typescriptlang.org/play/?ssl=1&amp;ssc=1&amp;pln=39&amp;pc=51#code/FDCWDsBcFMCcDMCGBjaACACo20poN7BpoDOAFttACYawD2ADgFymSwQDmANMAL4gQYCFOgDCZUABsqAQTTQAHjHBUSmSnnxoiaOuEkBPAJLgZLEm049%20YKHCSo04qVQBC8pblXqcm7cT1DE1cWcABXAFsAIzg%20EGQ9C3koUEhQaBIWAApnaRkAH1y3AEoAbQBdNABeNFKdQmJickoaemY0ACIFbo6eRt19Y1MWDpjkRDCSdCMItEQOnV4uAh0mihxWxhHuhV7VgaDwELQARgAmAGYAFgBWReBy%20MTINFASIrkarIYNSBYsXyQYosH6A15qD7VAB8K36OEgYVg4DQ31%20cwhEjyxQAdIEhnIAIRVGphFTQeAQagAbjiwAS4CSbyK7i%20oNwfx87OBaDZeDeTkxbmhsMa8MRyNRYMQGJcrhxeOCaCJJLJFPA1Np9KSyEFvgA6qkyKI4JBEBAAMrrai0RjVFHNDY29oWdjgDjcj4VYUNUXQBFI5JpNIZfb9bEUyRCLJMwUyYqhxrhqRR9BVGHQbEO61tarE0hWzYMYo0mxauiSDOSOgcLI6lz6w3G2Cmi0Fp1ZADkOw7xWLQA" rel="nofollow noreferrer">Here is a demo.</a></p>
  
  <p>But as you can see I put the return as <code>ChildA[]</code> and I could do the same with <code>ChildB[]</code>.</p>
  
  <p>I want something resembling the following which I could not make work:</p>
  
  <pre><code>const childrenWithCertainSharedProp = &lt;T&gt;(sharedProp: string): T[] =&gt; {
      return entities
          .filter(/* filter depending on T */)
          .filter(e =&gt; e.sharedProp === sharedProp);
  }
  </code></pre>
  
  <p>Basically what I want is somehow to type guard generically, in a way that I make sure that if I pass <code>&lt;ChildA&gt;</code> I will make sure it will filter only <code>ChildA</code> and so on.</p>