This utility type unwraps the type contained within a Promise. If the type is not a Promise, it returns the type unchanged. Note: TypeScript 4.5+ includes a built-in Awaited type with similar functionality.
type AsyncString = Promise<string>;
type SyncString = string;
// Extracts 'string' from Promise<string>
type Result1 = Awaited<AsyncString>; // string
// Returns 'string' unchanged since it's not a Promise
type Result2 = Awaited<SyncString>; // string
// Practical usage with async functions
async function fetchUser(): Promise<{ id: string; name: string }> {
// ...
}
type User = Awaited<ReturnType<typeof fetchUser>>; // { id: string; name: string }
Extracts the return type of a promise