Unwrap Promise types recursively with TypeScript. Learn conditional types, infer keyword, and recursive types in this easy-level challenge on TypeScriptPro.
Unwrap nested Promises to get their resolved types! 🎁
In this easy-level challenge, you'll implement a type that extracts the value type from Promises, even when they're nested multiple levels deep. This is similar to TypeScript's built-in Awaited
type and is essential for working with asynchronous code at the type level.
You'll learn about conditional types with infer
, recursive type definitions, and how to handle Promise-like objects with then
methods. This challenge bridges the gap between TypeScript's type system and JavaScript's asynchronous patterns.
For this challenge, you will need to change the following code to make the tests pass (no type check errors).
If we have a type which is a wrapped type like Promise, how can we get the type which is inside the wrapped type?
For example: if we have Promise<ExampleType>
how to get ExampleType?
type ExampleType = Promise<string>
type Result = MyAwaited<ExampleType> // string
This question is ported from the original article by @maciejsikora
To unwrap Promise types, we need to use conditional types with infer
and handle recursion for nested Promises.
type MyAwaited<T> = T extends PromiseLike<infer Value> ? MyAwaited<Value> : T
How it works:
PromiseLike<infer Value>
matches any object with a then
method and captures its resolved typeThis handles all cases including Promise<string>
, Promise<Promise<number>>
, and custom thenable objects, making it a robust solution for async type unwrapping.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount
This challenge is originally from here.