As someone new to TypeScript, I have some knowledge about the concept of generics in Java. Here's my dilemma: I have three functions - searchTrack, searchAlbum, and searchArtist.
searchTrack(query: string): Observable<Track[]> {
return this.search(query, 'track');
}
searchArtist(query: string): Observable<Artist[]> {
return this.search(query, 'artist');
}
searchAlbum(query: string): Observable<Album[]> {
return this.search(query, 'album');
}
I am looking for a general function named 'search' within this class that can take the query and the type of entity as parameters, and then return an observable collection of a specific entity type. However, I am unsure how to utilize generics in order to specify a generic return type for a function.
search(query: string, type: string): Observable<Array<T>> {
return this.query(`/search`, [
`q=${query}`,
`type=${type}`
]);
}
I'm seeking guidance on whether there is a way to achieve this functionality?