Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
b9b4565
commit ae34d9e
Showing
4 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {PromiseValue} from './promise-value'; | ||
|
||
type AsyncFunction = (...args: unknown[]) => Promise<unknown>; | ||
|
||
/** | ||
Unwrap the return type of a function that returns a `Promise`. | ||
There has been [discussion](https://github.com/microsoft/TypeScript/pull/35998) about implementing this type in TypeScript. | ||
@example | ||
```ts | ||
import {AsyncReturnType} from 'type-fest'; | ||
import {asyncFunction} from 'api'; | ||
// This type resolves to the unwrapped return type of `asyncFunction`. | ||
type Value = AsyncReturnType<typeof asyncFunction>; | ||
async function doSomething(value: Value) {} | ||
asyncFunction().then(value => doSomething(value)); | ||
``` | ||
*/ | ||
export type AsyncReturnType<Target extends AsyncFunction> = PromiseValue<ReturnType<Target>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import {AsyncReturnType} from '..'; | ||
|
||
async function asyncFunction(): Promise<number> { | ||
return Promise.resolve(2); | ||
} | ||
|
||
type Value = AsyncReturnType<typeof asyncFunction>; | ||
|
||
asyncFunction().then(value => { | ||
expectType<Value>(value); | ||
expectError<string>(value); | ||
}); |