0Pricing
TypeScript Academy · Lesson

Practical infer Use Cases: Unwrapping Promises

Unwrap Promise, Array, and custom wrapper types.

The Unwrapping Problem

When working with async code, you often receive Promise and need the underlying T. The infer keyword makes this extraction reusable.

type Awaited<T> = T extends Promise<infer U> ? U : T;
type A = Awaited<Promise<string>>; // string
type B = Awaited<number>;          // number

Built-in Awaited Utility

TypeScript 4.5 introduced the built-in Awaited that handles nested promises recursively.

type A = Awaited<Promise<Promise<string>>>; // string
// Handles arbitrary nesting automatically

All lessons in this course

  1. Understanding infer in Conditional Types
  2. Building ReturnType and Parameters from Scratch
  3. Deeply Nested Inference Patterns
  4. Practical infer Use Cases: Unwrapping Promises
← Back to TypeScript Academy