Unwrapping Promise Types
What you'll learn: How to extract the resolved value type from a Promise using conditional types with infer.
The Problem
When you have a Promise<string>, sometimes you need to work with just the inner string type itself — not the whole promise wrapper. TypeScript doesn't give you a built-in way to "unwrap" that inner type... or does it?
The Solution: Conditional Types with infer
Using what you've learned about conditional types and infer, you can extract the resolved value type from a promise:
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
type A = UnwrapPromise<Promise<string>>; // string
type B = UnwrapPromise<Promise<number>>; // number
type C = UnwrapPromise<boolean>; // boolean (not a promise, returns as-is)
How It Works
The conditional type asks: "Does T extend Promise<something>?" If yes, infer U captures that "something" (the resolved type) and returns it. If no, we just return T unchanged.
Think of it like unwrapping a gift box: if you receive a wrapped present, you extract what's inside. If someone hands you an unwrapped item, you just accept it directly.
Why This Matters
This pattern is crucial when building utilities that work with async functions or chained promises. You can programmatically determine what type a promise will resolve to without manually annotating it everywhere.
Key Takeaway: Conditional types with infer in the extends clause let you extract the resolved value type from Promise<T>, creating powerful type-level utilities for async code.