Categories
TypeScript Answers

How to unwrap the type of a Promise with TypeScript?

Spread the love

Sometimes, we want to unwrap the type of a Promise with TypeScript.

In this article, we’ll look at how to unwrap the type of a Promise with TypeScript.

How to unwrap the type of a Promise with TypeScript?

To unwrap the type of a Promise with TypeScript, we can use the Awaited type.

For instance, we write

type T = Awaited<Promise<PromiseLike<number>>>;

to create type T that is set to Awaited<Promise<PromiseLike<number>>>.

We use Awaited with Promise<PromiseLike<number>> to return the data type of the resolved value of a promise like object that we use await with.

Therefore T is number.

Conclusion

To unwrap the type of a Promise with TypeScript, we can use the Awaited type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *