Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | /**
|
---|
2 | Returns the type that is wrapped inside a `Promise` type.
|
---|
3 | If the type is a nested Promise, it is unwrapped recursively until a non-Promise type is obtained.
|
---|
4 | If the type is not a `Promise`, the type itself is returned.
|
---|
5 |
|
---|
6 | @example
|
---|
7 | ```
|
---|
8 | import {PromiseValue} from 'type-fest';
|
---|
9 |
|
---|
10 | type AsyncData = Promise<string>;
|
---|
11 | let asyncData: PromiseValue<AsyncData> = Promise.resolve('ABC');
|
---|
12 |
|
---|
13 | type Data = PromiseValue<AsyncData>;
|
---|
14 | let data: Data = await asyncData;
|
---|
15 |
|
---|
16 | // Here's an example that shows how this type reacts to non-Promise types.
|
---|
17 | type SyncData = PromiseValue<string>;
|
---|
18 | let syncData: SyncData = getSyncData();
|
---|
19 |
|
---|
20 | // Here's an example that shows how this type reacts to recursive Promise types.
|
---|
21 | type RecursiveAsyncData = Promise<Promise<string> >;
|
---|
22 | let recursiveAsyncData: PromiseValue<RecursiveAsyncData> = Promise.resolve(Promise.resolve('ABC'));
|
---|
23 | ```
|
---|
24 | */
|
---|
25 | export type PromiseValue<PromiseType, Otherwise = PromiseType> = PromiseType extends Promise<infer Value>
|
---|
26 | ? { 0: PromiseValue<Value>; 1: Value }[PromiseType extends Promise<unknown> ? 0 : 1]
|
---|
27 | : Otherwise;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.