Generic return type of function in Typescript

Param Singh

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

  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 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.

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

Is there any way I can achieve this?

toskv

Try using the Array class instead of []. Also define a generic T type on the search function.

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

You should be able to call it like this:

let result = search<Artist>('someQuery', 'artist');

You can find more about generics in typescript in the Generics chapter in the handbook here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Specify return type in TypeScript arrow function

From Java

Generic function where the return type depends on the input type in Scala?

From Java

TypeScript function return type based on input parameter

From Java

TypeScript: Is it possible to get the return type of a generic function?

From Dev

TypeScript Compiler API: Get resolved return type of a generic method

From Dev

void as a type of an argument of a generic function in TypeScript

From Dev

return type for class function typescript

From Dev

Swift generic function calling function with return type overload

From Dev

Creating a generic LINQ function to return the type that is passed in

From Dev

Scala: having a function with generic return type

From Dev

Using Generic.List with custom type as a return type for function is not working

From Dev

Typescript: generic that extends a type with a generic

From Dev

Typescript: Constrain function generic type based on the expected return type

From Dev

Swift protocol generic as function return type

From Dev

How to constrain the type of generic parameter to a Typescript function to be an enum

From Dev

TypeScript return exact function infer type

From Dev

Infer function generic type U from return value of passed function

From Dev

Can I use conditional generic to set a callback return type in typescript?

From Dev

Typescript - Function return type if I want to return object

From Dev

Implementing a function with conditional return type in Typescript

From Dev

Typescript throws error for return function type

From Dev

Return type issue in generic function using conditional type

From Dev

TypeScript generic type and default function application

From Dev

return a generic function with specified type

From Dev

(TypeScript) How to capture the type provided by the user inside the generic function?

From Dev

Swift: check return type of generic function

From Dev

Return HashSet<T> from HashSet of generic type in generic function

From Dev

Kotlin - abstract function with generic return type

From Dev

Call static function of Generic Type from Generic Type in typescript

Related Related

  1. 1

    Specify return type in TypeScript arrow function

  2. 2

    Generic function where the return type depends on the input type in Scala?

  3. 3

    TypeScript function return type based on input parameter

  4. 4

    TypeScript: Is it possible to get the return type of a generic function?

  5. 5

    TypeScript Compiler API: Get resolved return type of a generic method

  6. 6

    void as a type of an argument of a generic function in TypeScript

  7. 7

    return type for class function typescript

  8. 8

    Swift generic function calling function with return type overload

  9. 9

    Creating a generic LINQ function to return the type that is passed in

  10. 10

    Scala: having a function with generic return type

  11. 11

    Using Generic.List with custom type as a return type for function is not working

  12. 12

    Typescript: generic that extends a type with a generic

  13. 13

    Typescript: Constrain function generic type based on the expected return type

  14. 14

    Swift protocol generic as function return type

  15. 15

    How to constrain the type of generic parameter to a Typescript function to be an enum

  16. 16

    TypeScript return exact function infer type

  17. 17

    Infer function generic type U from return value of passed function

  18. 18

    Can I use conditional generic to set a callback return type in typescript?

  19. 19

    Typescript - Function return type if I want to return object

  20. 20

    Implementing a function with conditional return type in Typescript

  21. 21

    Typescript throws error for return function type

  22. 22

    Return type issue in generic function using conditional type

  23. 23

    TypeScript generic type and default function application

  24. 24

    return a generic function with specified type

  25. 25

    (TypeScript) How to capture the type provided by the user inside the generic function?

  26. 26

    Swift: check return type of generic function

  27. 27

    Return HashSet<T> from HashSet of generic type in generic function

  28. 28

    Kotlin - abstract function with generic return type

  29. 29

    Call static function of Generic Type from Generic Type in typescript

HotTag

Archive