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.