Middle+
What is the Awaited utility type in TypeScript?
sobes.tech AI
Answer from AI
In TypeScript, Awaited is a utility type that allows you to obtain the type of the value that a Promise resolves to.
If you have a type Promise<T>, then Awaited<Promise<T>> will be T. This is useful for extracting the result type of an asynchronous function.
Example:
type ResultType = Awaited<Promise<string>>; // ResultType will be string
async function fetchData(): Promise<number> {
return 42;
}
type Data = Awaited<ReturnType<typeof fetchData>>; // Data will be number
Awaited also works correctly with nested promises, for example, Promise<Promise<T>> will be unwrapped to T.