Awaited
Since 1.0.0
Recursively unwrap Promise types.
Signature
typescript
type Awaited<T> = T extends Promise<infer U> ? Awaited<U> : TParameters
| Parameter | Description |
|---|---|
T | The Promise type to unwrap |
Description
Awaited is similar to TypeScript's built-in Awaited type, but documented here for completeness. It recursively unwraps nested Promises to get the final value type.
Examples
Basic Usage
typescript
import type { Awaited } from 'uni-types'
type Result1 = Awaited<Promise<string>> // string
type Result2 = Awaited<Promise<number>> // numberNested Promises
typescript
type Result3 = Awaited<Promise<Promise<string>>> // string
type Result4 = Awaited<Promise<Promise<Promise<boolean>>>> // booleanNon-Promise Types
typescript
type Result5 = Awaited<string> // string
type Result6 = Awaited<number> // numberWith Async Functions
typescript
async function fetchUser(): Promise<{ name: string; email: string }> {
// ...
}
type User = Awaited<ReturnType<typeof fetchUser>>
// { name: string; email: string }Related
ArrayElement- Get array element typeValueOf- Get object value types