source: trip-planner-front/node_modules/type-fest/source/asyncify.d.ts@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import {PromiseValue} from './promise-value';
2import {SetReturnType} from './set-return-type';
3
4/**
5Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
6
7Use-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.
8
9@example
10```
11import {Asyncify} from 'type-fest';
12
13// Synchronous function.
14function getFooSync(someArg: SomeType): Foo {
15 // …
16}
17
18type AsyncifiedFooGetter = Asyncify<typeof getFooSync>;
19//=> type AsyncifiedFooGetter = (someArg: SomeType) => Promise<Foo>;
20
21// Same as `getFooSync` but asynchronous.
22const 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.".
26
27 // …
28}
29```
30*/
31export type Asyncify<Fn extends (...args: any[]) => any> = SetReturnType<Fn, Promise<PromiseValue<ReturnType<Fn>>>>;
Note: See TracBrowser for help on using the repository browser.