-
-
Notifications
You must be signed in to change notification settings - Fork 543
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
8b857ce
commit 7fe70fd
Showing
4 changed files
with
36 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,20 @@ | ||
/** | ||
Returns the type that is wrapped inside a `Promise` type. | ||
If the type is not a `Promise`, the type itself is returned. | ||
@example | ||
``` | ||
import {PromiseValue} from 'type-fest'; | ||
type AsyncData = Promise<string>; | ||
let asyncData: PromiseValue<AsyncData> = Promise.resolve('ABC'); | ||
type Data = PromiseValue<AsyncData>; | ||
let data: Data = await asyncData; | ||
// Here's an example that shows how this type reacts to non-Promise types. | ||
type SyncData = PromiseValue<string>; | ||
let syncData: SyncData = getSyncData(); | ||
``` | ||
*/ | ||
export type PromiseValue<PromiseType, Otherwise = PromiseType> = PromiseType extends Promise<infer Value> ? Value : Otherwise; |
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,14 @@ | ||
import {expectType} from 'tsd'; | ||
import {PromiseValue} from '..'; | ||
|
||
type NumberPromise = Promise<number>; | ||
type Otherwise = object; | ||
|
||
// Test the normal behaviour. | ||
expectType<PromiseValue<NumberPromise>>(2); | ||
|
||
// Test what happens when the `PromiseValue` type is not handed a `Promise` type. | ||
expectType<PromiseValue<number>>(2); | ||
|
||
// Test the `Otherwise` generic parameter. | ||
expectType<PromiseValue<number, Otherwise>>({}); |