Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReturnType<typeof F> where F is a function that returns type Promise<T>

I want to set the type of a variable to T.

If F was a function that did NOT return a promise and just returned an object of type T, I would just do something like this: let x: ReturnType<typeof F>

But F is a function that returns a Promise<T>

How can I do this?

like image 442
Ayush Avatar asked Jan 24 '26 12:01

Ayush


1 Answers

You can adapt the example given in the section Type inference in conditional types of Advanced Types chapter of the Typescript manual

type Unpacked<T> =
  T extends (...args: any[]) => infer U ? U :
  T extends Promise<infer U> ? U :
  T;

For example for fetch you would get

type T = Unpacked<Unpacked<typeof fetch>>
// Response
like image 99
Harald Gliebe Avatar answered Jan 27 '26 01:01

Harald Gliebe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!