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

jsdw

I have exported a function from some module that looks like:

export function MyFunc<A>() {
    return {
        foo: (in: A) => void
    }
}

Now, in some other module, I want to be able to talk about the different return types of MyFunc. Since I didn't export the type, I'll use typeof to get hold of the type I want given the value MyFunc. Ideally I would do the following:

import { MyFunc } from "mymodule";
type MyFuncReturned<A> = ReturnType<typeof MyFunc<A>>;

function foo(): MyFuncReturned<string> {
   // ...
}

Hrmph, this doesn't work; typeof can only be passed a value and doesn't like my attempt to specify the generic type of that value.

The best I can do is convincing TypeScript to infer specific types of MyFunc from values I've created, and then giving them individual type aliases, eg:

const myFuncStringReturn = MyFunc<string>();
type MyFuncStringReturn = typeof myFuncStringReturn;

To avoid actually running MyFunc just to get the type info, I can hide it behind a function and use ReturnType on it:

const myFuncStringReturn = () => MyFunc<string>();
type MyFuncStringReturn = ReturnType<typeof myFuncStringReturn>;

const myFuncBoolReturn = () => MyFunc<bool>();
type MyFuncBoolReturn = ReturnType<typeof myFuncBoolReturn>;

This gives me a way of, one type at a time, talking about the different return types of MyFunc, but it

  • Involves actual runtime code to be written that TS can infer from.
  • Doesn't let me talk about MyFunc in a more generic sense.

The only "proper" solution I can come up with is duplicating a bunch of type info when I declare MyFunc:

export function MyFunc<A>(): MyFuncReturns<A> {
    return {
        foo: (in: A) => void
    }
}

export type MyFuncReturns<A> = {
    foo: (in: A) => void
}

But now as I change MyFunc, I have to make sure to keep MyFuncReturns in sync with it.

Is there any way I can get hold of a type like MyFuncReturns<A> given just our exported value MyFunc, without having to add runtime code or add the boilerplate above?

Titian Cernicova-Dragomir

There is a proposal to allow using typeof with arbitrary expressions to allow things like getting the return type of a generic functions for a specific type argument (see here and here)

A more generic workaround that works today is to use a generic class with a field that is tied to the return type of the function. We can then extract the field of the class. Since for classes we can specify generic type parameters in type expressions we can extract the generic form of the return type:

export function MyFunc<A>() {
  return {
    foo: (os : A) => {}
  }
}

class Helper <T> {
  Return = MyFunc<T>()
}
type FuncReturnType<T> = Helper<T>['Return']
type ForBool = FuncReturnType<boolean> //  {foo: (os: boolean) => void;}
type ForString = FuncReturnType<string> //  {foo: (os: string) => void;}

Note If you have constraints of A you will need to duplicate those on T in Helper and FuncReturnType, that is unavoidable unfortunately.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic return type of function in Typescript

From Dev

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

From Dev

Is it possible to get a return type from a keyup function?

From Dev

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

From Dev

Are generic type aliases possible in TypeScript?

From Dev

Is it possible in typescript to have a function return the type of a class that extends it

From Dev

return a generic function with specified type

From Dev

Generic return type possible with Spring AOP

From Dev

return type for class function typescript

From Dev

Get generic type from a Function

From Dev

Is it possible to have non-generic method return generic type?

From Dev

Call static function of Generic Type from Generic Type in typescript

From Dev

Possible to make a generic type checking function in Swift?

From Dev

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

From Dev

TypeScript generic type and default function application

From Dev

Scala: having a function with generic return type

From Dev

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

From Dev

Swift protocol generic as function return type

From Dev

Swift: check return type of generic function

From Dev

Kotlin - abstract function with generic return type

From Dev

Is it possible to get the type from a generic, given as parameter

From Dev

Get constructor/instance from generic type in TypeScript

From Dev

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

From Dev

Is it possible to create a type alias that has trait bounds on a generic type for a function?

From Dev

Is it possible to check if the function has a void return type

From Dev

Is it possible to change return type of a specialized template function?

From Dev

Is it possible to know the type of return value of a function in Go?

From Dev

A function that can return an object or a primitive type: is it possible?

From Java

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

Related Related

  1. 1

    Generic return type of function in Typescript

  2. 2

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

  3. 3

    Is it possible to get a return type from a keyup function?

  4. 4

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

  5. 5

    Are generic type aliases possible in TypeScript?

  6. 6

    Is it possible in typescript to have a function return the type of a class that extends it

  7. 7

    return a generic function with specified type

  8. 8

    Generic return type possible with Spring AOP

  9. 9

    return type for class function typescript

  10. 10

    Get generic type from a Function

  11. 11

    Is it possible to have non-generic method return generic type?

  12. 12

    Call static function of Generic Type from Generic Type in typescript

  13. 13

    Possible to make a generic type checking function in Swift?

  14. 14

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

  15. 15

    TypeScript generic type and default function application

  16. 16

    Scala: having a function with generic return type

  17. 17

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

  18. 18

    Swift protocol generic as function return type

  19. 19

    Swift: check return type of generic function

  20. 20

    Kotlin - abstract function with generic return type

  21. 21

    Is it possible to get the type from a generic, given as parameter

  22. 22

    Get constructor/instance from generic type in TypeScript

  23. 23

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

  24. 24

    Is it possible to create a type alias that has trait bounds on a generic type for a function?

  25. 25

    Is it possible to check if the function has a void return type

  26. 26

    Is it possible to change return type of a specialized template function?

  27. 27

    Is it possible to know the type of return value of a function in Go?

  28. 28

    A function that can return an object or a primitive type: is it possible?

  29. 29

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

HotTag

Archive