1 import {PromiseValue} from './promise-value';
2 import {SetReturnType} from './set-return-type';
5 Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
7 Use-case: You have two functions, one synchronous and one asynchronous that do the same thing. Instead of having to duplicate the type definition, you can use `Asyncify` to reuse the synchronous type.
11 import {Asyncify} from 'type-fest';
13 // Synchronous function.
14 function getFooSync(someArg: SomeType): Foo {
18 type AsyncifiedFooGetter = Asyncify<typeof getFooSync>;
19 //=> type AsyncifiedFooGetter = (someArg: SomeType) => Promise<Foo>;
21 // Same as `getFooSync` but asynchronous.
22 const getFooAsync: AsyncifiedFooGetter = (someArg) => {
23 // TypeScript now knows that `someArg` is `SomeType` automatically.
24 // It also knows that this function must return `Promise<Foo>`.
25 // If you have `@typescript-eslint/promise-function-async` linter rule enabled, it will even report that "Functions that return promises must be async.".
31 export type Asyncify<Fn extends (...args: any[]) => any> = SetReturnType<Fn, Promise<PromiseValue<ReturnType<Fn>>>>;