Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

28 rindas
1.0KB

  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. @example
  6. ```
  7. import {PromiseValue} from 'type-fest';
  8. type AsyncData = Promise<string>;
  9. let asyncData: PromiseValue<AsyncData> = Promise.resolve('ABC');
  10. type Data = PromiseValue<AsyncData>;
  11. let data: Data = await asyncData;
  12. // Here's an example that shows how this type reacts to non-Promise types.
  13. type SyncData = PromiseValue<string>;
  14. let syncData: SyncData = getSyncData();
  15. // Here's an example that shows how this type reacts to recursive Promise types.
  16. type RecursiveAsyncData = Promise<Promise<string> >;
  17. let recursiveAsyncData: PromiseValue<RecursiveAsyncData> = Promise.resolve(Promise.resolve('ABC'));
  18. ```
  19. */
  20. export type PromiseValue<PromiseType, Otherwise = PromiseType> = PromiseType extends Promise<infer Value>
  21. ? { 0: PromiseValue<Value>; 1: Value }[PromiseType extends Promise<unknown> ? 0 : 1]
  22. : Otherwise;