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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With