Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the return type of a function which uses generics

Disclaimer: overly simplified functions follow, I'm aware they're useless

function thinger<T>(thing: T): T {
    return thing;
}

const thing = thinger({ a: "lol" });

thing.a;

The above code transpiles just fine. But I need to place result of thinger<T> into an object.

interface ThingHolder {
    thing: ReturnType<typeof thinger>;
}

const myThingHolder: ThingHolder = {
    thing: thinger({ a: "lol" }),
};

However I have lost my type information so myThingHolder.thing.a does not work

Property 'a' does not exist on type '{}'

So I tried the following

interface ThingHolder<T> {
    thing: ReturnType<typeof thinger<T>>;
}

const myThingHolder: ThingHolder<{ a: string }> = {
    thing: thinger({ a: "lol" }),
};

But typeof thinger<T> is not valid typescript.

How can I get the return type of a function which has a different return type based on generics?

like image 498
ed' Avatar asked Apr 24 '18 15:04

ed'


People also ask

How do you define generic return type?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

How do you find the return type of a function in TypeScript?

Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.

Is it possible to return a generic type in Java?

(Yes, this is legal code; see Java Generics: Generic type defined as return type only.) The return type will be inferred from the caller.

What is ReturnType in TypeScript?

The ReturnType in TypeScript is a utility type which is quite similar to the Parameters Type. It let's you take the return output of a function, and construct a type based off it.


1 Answers

I might as well put this in an answer although it doesn't look like it will meet your needs. TypeScript currently has neither generic values, higher kinded types, nor typeof on arbitrary expressions. Generics in TypeScript are sort of "shallow" that way. So as far as I know there's unfortunately no way to describe a type function that plugs type parameters into generic functions and checks the result:

// doesn't work, don't try it
type GenericReturnType<F, T> = F extends (x: T) => (infer U) ? U : never

function thinger<T>(thing: T): T {
  return thing;
}

// just {}, 🙁
type ReturnThinger<T> = GenericReturnType<typeof thinger, T>;

So all I can do for you is suggest workarounds. The most obvious workaround would be to use a type alias to describe what thinger() returns, and then use it multiple places. This is a "backwards" version of what you want; instead of extracting the return type from the function, you build the function from the return type:

type ThingerReturn<T> = T; // or whatever complicated type you have

// use it here
declare function thinger<T>(thing: T): ThingerReturn<T>;

// and here
interface ThingHolder<T> {
  thing: ThingerReturn<T>;
}

// and then this works 🙂 
const myThingHolder: ThingHolder<{ a: string }> = {
  thing: thinger({ a: "lol" }),
};

Does that help? I know it's not what you wanted, but hopefully it's at least a possible path forward for you. Good luck!

like image 126
jcalz Avatar answered Oct 12 '22 21:10

jcalz