Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
Awaited is a utility type in TypeScript introduced in version 4.5 that extracts the value from a Promise. If a function or variable returns Promise<T>, then Awaited<T> will give T itself.
Awaited<T>
T — type that can be a promise or value.T is a promise (Promise<U>), then Awaited<T> returns type U.T is not a promise, then Awaited<T> returns T itself.async function getData(): Promise<number> {
return 42;
}
type DataType = Awaited<ReturnType<typeof getData>>;
// DataType → number
In this example:
fetchData returns Promise<number>.ReturnType<typeof fetchData> extracts function's return type, i.e. Promise<number>.Awaited<Promise<number>> unwraps promise, returning type number.type NestedPromise = Promise<Promise<string>>;
type Result = Awaited<NestedPromise>;
// Result → string
Simplifying work with async functions
Awaited allows explicitly specifying result type obtained after awaiting promise, simplifying typing and reducing duplication.
Recursive type extraction
When working with nested promises, Awaited automatically "unwraps" nested types, returning final value.
Support for async utilities
Awaited is useful when building generic utilities for async code where knowing exact data type after promise resolution is important.
T is not a promise, Awaited simply returns T unchanged.Awaited is a powerful utility type that allows automatically extracting value types from promises. It's especially useful for asynchronous programming in TypeScript, providing accurate typing of returned data and simplifying work with nested promises.