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