What is the Awaited utility type for?
Awaited<T> gives the type you get after awaiting a promise, unwrapping nested promises too. Awaited<Promise<string>> is string. It is mostly used with ReturnType to get the resolved type of an async function.
Explain it simply
A courier tracking number is a promise of a parcel, not the parcel. await is the step where you receive the parcel. Awaited<T> is the type-level version: start from the tracking number type, end with the parcel type.
It also handles layers: a promise of a promise unwraps all the way down, the same as await does at runtime. Non-promise types pass through unchanged.
Example
type A = Awaited<Promise<string>>; // string
type B = Awaited<Promise<Promise<number>>>; // number
type C = Awaited<boolean>; // boolean (unchanged)
async function fetchOrders() {
return [{ id: 1, total: 499 }];
}
type OrdersPromise = ReturnType<typeof fetchOrders>;
// Promise<{ id: number; total: number }[]>
type Orders = Awaited<ReturnType<typeof fetchOrders>>;
// { id: number; total: number }[]
async function run() {
const orders: Orders = await fetchOrders();
console.log(orders.length);
}What to say in the interview
"Awaited<T> is the type-level await: it unwraps promises, including nested ones, and leaves other types alone. Combined with ReturnType it lets me type the resolved result of an async function without writing the shape twice."