Generic return type of function in Typescript
<p>I'm new to ts but I've learnt a little about the concept of generics in java. The query is that I have three functions : searchTrack, searchAlbum, searchArtist </p>

<pre><code>  searchTrack(query: string): Observable&lt;Track[]&gt; {
    return this.search(query, 'track');
  }

  searchArtist(query: string): Observable&lt;Artist[]&gt; {
    return this.search(query, 'artist');
  }

  searchAlbum(query: string): Observable&lt;Album[]&gt; {
    return this.search(query, 'album');
  }
</code></pre>

<p>I want a general function 'search' in this class that takes the query and the type of entity and returns an Observable of collection of a specific entity type. I'm stuck here. How can I work with generics to specify a generic return type of a function.</p>

<pre><code>search(query: string, type: string): Observable&lt;Array&lt;T&gt;&gt; {
 return this.query(`/search`, [
   `q=${query}`,
   `type=${type}`
 ]);
}
</code></pre>

<p>Is there any way I can achieve this?</p>