-
-
Notifications
You must be signed in to change notification settings - Fork 573
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 <[email protected]>
- Loading branch information
1 parent
cfac1de
commit 670462c
Showing
4 changed files
with
30 additions
and
0 deletions.
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 @@ | ||
/** | ||
Create a type that represents either the value or the value wrapped in `PromiseLike`. | ||
Use-cases: | ||
- A function accepts a callback that may either return a value synchronously or may return a promised value. | ||
- This type could be the return type of `Promise#then()`, `Promise#catch()`, and `Promise#finally()` callbacks. | ||
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394) if you want to have this type as a built-in in TypeScript. | ||
@example | ||
``` | ||
import {Promisable} from 'type-fest'; | ||
async function logger(getLogEntry: () => Promisable<string>): Promise<void> { | ||
const entry = await getLogEntry(); | ||
console.log(entry); | ||
} | ||
logger(() => 'foo'); | ||
logger(() => Promise.resolve('bar')); | ||
``` | ||
*/ | ||
export type Promisable<T> = T | PromiseLike<T>; |
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,5 @@ | ||
import {expectType} from 'tsd'; | ||
import {Promisable} from '..'; | ||
|
||
declare const promisable: Promisable<string>; | ||
expectType<PromiseLike<string> | string>(promisable); |