Awaitable type
A value that is returned directly or through a promise: the result of a callback that may be sync or async.
ts
import type { Awaitable } from 'utils';Signature
ts
export type Awaitable<T> = PromiseLike<T> | T;Parameters
| Name | Description |
|---|---|
T | The value type. |
Example
ts
type Loader = (page: number) => Awaitable<readonly Row[]>;Tests
Line coverage: 0 %.
- ✅ accepts a value or a promise
Source
src/types/awaitable.ts (on GitHub)
Source code
ts
/**
* A value that is returned directly or through a promise: the result of a callback that may be sync or
* async.
*
* @template T - The value type.
* @example
* type Loader = (page: number) => Awaitable<readonly Row[]>;
*/
export type Awaitable<T> = PromiseLike<T> | T;Test file
ts
import type { Awaitable } from './awaitable';
describe('Awaitable', () => {
it('accepts a value or a promise', () => {
expectTypeOf<number>().toExtend<Awaitable<number>>();
expectTypeOf<Promise<number>>().toExtend<Awaitable<number>>();
expectTypeOf<Promise<string>>().not.toExtend<Awaitable<number>>();
});
});Keywords: awaitable, types, type