title: Typescript: how to pass in a generic function as a function parameter?
body: <p>At the moment, I have a function that successfully finds one element in a given array and shifts it to the front. But I want to define a function that given an array of values, it will iterate through these, find them and shift them to the front of an array (which is passed as another parameter). The part that I'm stuck on right now is, the finding aspect. Each value that is passed (to be shifted to the front) needs to be found in the array first.</p>
  
  <p>To do this part, I wanted to have that find parameter as a function parameter. </p>
  
  <p>My first attempt was something along these lines (I know this does not work):</p>
  
  <pre><code>public shiftObjectsToStart(array: Array&lt;any&gt;, itemsToShift: Array&lt;any&gt;, findIndex: (...args: any[]) =&gt; number): void {
      itemsToShift.forEach(element =&gt; {
          const idx = findIndex(); // This needs to be changed/fixed/HELP
          if (idx !== -1){
              this.shiftObjectToStart(array, idx);
          }
      });
  }
  </code></pre>
  
  <p>I am sure that I'm getting my wires crossed in my approach to make the function more generic feeling by making the find aspect a function. </p>
  
  <p>Keep in mind that I'm mostly using these Array functions with custom defined objects, whereby finding instances of said object in a collection will vary.</p>
  
  <p><strong>My code MUST be ES5 friendly</strong></p>
  
  <p>The full array utils service can be found here:
  <a href="https://gist.github.com/richflee/9fead9819ab016f27ee9582326858f90" rel="nofollow noreferrer">https://gist.github.com/richflee/9fead9819ab016f27ee9582326858f90</a></p>