A collection of utility types for handling async functions and Promises.
ApiAwaitable<T>
Unifies the return type of synchronous and asynchronous operations.
Source
export type ApiAwaitable<T> = T | Promise<T>
Example
import type { ApiAwaitable } from '@movk/core'
// a: string | Promise<string>
let a: ApiAwaitable<string>
ApiUnwrapPromise<T>
Extracts the type contained within a Promise.
Source
export type ApiUnwrapPromise<T> = T extends Promise<infer U> ? U : T
Example
import type { ApiUnwrapPromise } from '@movk/core'
type P = Promise<number>
// U is number
type U = ApiUnwrapPromise<P>
ApiAwaitedReturn<TFn>
Extracts the return type of an async function.
Source
export type ApiAwaitedReturn<TFn> = TFn extends (...args: any[]) => ApiAwaitable<infer R> ? R : never
Example
import type { ApiAwaitedReturn } from '@movk/core'
type MyFn = () => Promise<string>
// R is string
type R = ApiAwaitedReturn<MyFn>
Changelog
No recent changes