TypeScriptUtility types
Awaited<Type>
Gets the type of the value that awaiting a promise produces.
Example
#TypeScript
async function loadCourses() {
return [
{ title: 'HTML and CSS', modules: 11 },
{ title: 'Python', modules: 12 }
]
}
type Courses = Awaited<ReturnType<typeof loadCourses>>
// type Courses = { title: string; modules: number }[]
const courses: Courses = await loadCourses()
console.log(courses.map((c) => c.title))Result
['HTML and CSS', 'Python']
Definition and usage
#Awaited<Type> unwraps promises the same way await does: Awaited<Promise<string>> is string. It also handles nested promises, unwrapping them all the way down.
It’s most often used with async functions: Awaited<ReturnType<typeof fetchCourses>> is the type of the data you actually get after await fetchCourses().
On types that aren’t promises, Awaited doesn’t change anything, so it can be safely used in generic types.
Syntax
#Syntax
type Value = Awaited<PromiseType>Type parameters
#Type
any type
The type to unwrap from promises.
Good practices
#- Remember the pattern
Awaited<ReturnType<typeof fn>>for async functions. - The type of
Promise.allon an array of promises is also computed byAwaitedfor each element.
Related entries
#Spotted a mistake or missing an example? Write to us.